Background:I have a Python script that runs (infinitely) from startup in the background of a Ubuntu server. The process is ended by sending a SIGINT which is handled by the script which finishes all jobs before exiting. If anyone were to shutdown the server, thus terminating the script, important data will be lost. I was wondering if there were a way to wait for the script to finish before shutdown, or prevent shutdown altogether (if preventing it'd be nice if it could display a message).Attempt:My Python script (test.py) has a SIGINT handler where when it receives a SIGINT, it finishes all tasks before exiting. I wrote the following shell script:PROCESS=$(pgrep -f 'python test.py')echo $PROCESSkill -2 $PROCESSwhile kill -2 $PROCESS 2> /dev/null; do        sleep 1doneThis script will continuously send kill commands to the python script until it exits (it works when run). I put the script in the /etc/init.d directory, executed chmod -x on the script, made symlinks to the /etc/rc0.d and /etc/rc6.d directories with names starting with K99. Since scripts in the /etc/rc0.d//etc/rc6.d directory get run at shutdown/reboot, the script should (theoretically) run and wait until the python script finishes to shutdown/reboot. This does not seem to be working. It shutsdown/reboots without finishing the tasks.Help would be greatly appreciated.
Prevent shutdown using shell script run at shutdown
shell script;ubuntu;shutdown;init.d;rc
You linked your script to K99. When changing runlevel, the K* scripts are called with option stop, and the S* scripts are called with option start, or stop for runlevel 0 (shutdown). They are called in numerical order. So you should remove your K99 links and replace them by K00 links so they are executed first (before the script that actually halts the system!).