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. ^c
ing 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.
ExecStop
is run after ubuntu closes down the java process. I verified that Java isn't running at the time by doing aexec $(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:38ExecStop
is run after Ubuntu closes all the processes, so it's a bit late. D= – The1nk Sep 02 '16 at 17:46