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:
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:
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:
- What's wrong with my while loop?
- 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.