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.
  • 1
    With systemd, you can use inhibitors. Might look it up later... – Byte Commander Sep 2 '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 2 '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: unix.stackexchange.com/a/294539/103151 – Byte Commander Sep 2 '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 2 '16 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.