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 $PROCESS
kill -2 $PROCESS
while kill -2 $PROCESS 2> /dev/null; do
sleep 1
done
This 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.