1

EDIT: The question that this is marked as a possible duplicate of offers a way for a script to prevent shutdown while it is running. My problem is that during the shutdown process, I want to trigger a script to run, and let it finish, before continuing the shutdown. I'm not sure inhibitors are applicable.


On system startup I have a rc.local file that opens Minecraft, this works:

/etc/rc.local:

su -l minecraftUser -c "screen -dmS minecraft /home/minecraftUser/server/wrapper.sh"

I'm trying to get a safe shutdown process going, so I can signal a shutdown and have it safely close Minecraft. This is what I have currently:

/etc/init.d/shutdownMinecraft.sh:

su -l minecraftUser -c "screen -S minecraft -p 0 -X stuff \"stop $(printf \\r)\""

while [ -e /proc/$(pidof java) ]; do sleep 0.1; done

echo "Done stopping server" >> /home/minecraftUser/server/logs/latest.log

and symbolic links in both rc0.d and rc6.d:

K01shutdownminecraft -> /etc/init.d/shutdownMinecraft.sh

.. However, it doesn't work. The log doesn't show the correct Minecraft shutdown messages (saving world) and also doesn't show that final echo. If I run the script manually (sudo ./shutdownMinecraft.sh), it hangs seemingly forever. ^cing out shows me Java did in fact close, and the server closed correctly -- but my while loop doesn't work as expected.

So, two questions:

  1. What's wrong with my while loop?
  2. How can I get Ubuntu to wait while this script is executing? I'd expect Ubuntu to not shutdown while my script is hanging infinitely ,but it does anyway.
The1nk
  • 111
  • 1
    With systemd, you can use inhibitors. Might look it up later... – Byte Commander Sep 02 '16 at 14:39
  • 1
  • @ByteCommander It looks like that is a method for preventing shutdown altogether when a script is running. I'm looking to a trigger a script to run, and let it finish, before shutdown. I'm not sure how the solutions offered in that question are applicable? – The1nk Sep 02 '16 at 14:48
  • 1
    Oh, I misread your question, sorry. Take a look at this answer though, it creates a service for systemd which allows you to run commands when it is meant to shut down: http://unix.stackexchange.com/a/294539/103151 – Byte Commander Sep 02 '16 at 14:52
  • 1
    What about a hook in the shutdown process to check if Minecraft is active and close it down in a orderly fashion? EDIT Byte commander just said same thing while I was typing. – WinEunuuchs2Unix Sep 02 '16 at 14:54
  • @ByteCommander - I followed through that and made a minecraft service, but now I'm having a different issue .. It seems as though the service's ExecStop is run after ubuntu closes down the java process. I verified that Java isn't running at the time by doing a exec $(pidof java) >> derp.txt, which gives me a blank line. I need it to run before ubuntu kills the processes, obviously. And thanks for your help so far! – The1nk Sep 02 '16 at 17:38
  • @WinEunuuchs2Unix How would you propose checking if it's still running ? Using the systemd services, or ? I'm seeing an issue where a services' ExecStop is run after Ubuntu closes all the processes, so it's a bit late. D= – The1nk Sep 02 '16 at 17:46
  • @The1nk I'm probably the only person here that doesn't play minecraft or use java so I'm the wrong person to ask. Twice today I've seen code that is looping to see if shut down has been signaled and then sleeps 1 second. It would make more sense to find the shut down script or code and send an Alt+F4 to the window which would prompt the user to save files before exiting. Give a 10 second pause before closing and loosing files? If Java is the parent and minecraft is a child process there should be something in java to send shutdown too. Shutdown is flawless for me so I've never researched it. – WinEunuuchs2Unix Sep 02 '16 at 22:34
  • Sorry, have never used those services myself, just googled and found that. No idea how to proceed. – Byte Commander Sep 03 '16 at 00:06
  • I have written something like that before. I'll add a link – Sergiy Kolodyazhnyy Sep 03 '16 at 01:19
  • Here it is, but it is for updates specifically. if you want, i can modify it to suit your needs: http://askubuntu.com/a/713590/295286 – Sergiy Kolodyazhnyy Sep 03 '16 at 01:21

0 Answers0