Long running bash update script loop. How to handle terminating better?

Rory asked:

I have a bash script that downloads some data, runs a process to import it, then sleeps for a little while (about an hour). It’s running fine as a systemd service (on Ubuntu 16.04 & 18.04)

But I am afraid that interrupting the import process could leave the database in an inconsistent state, so I want to prevent it being interrupted. i.e. systemctl stop mything should block until the import process is finished. Other steps in the process can be interrupted at will.

Is there any easy way in a bash script to run a command and say “don’t propagate the SIGKILL signal to this command”? (nohup blocks SIGHUP). For other, similar scripts, I’ve used bash’s trap to ‘catch’ the signal which stops bash killing the process, but then the sleep isn’t killed, so I had to run sleep in the background and wait on it (and then kill the sleep in the signal handler). Which seems overtly complicated.

Is there a better way? Is there a nosigkill command (like nohup) which will block sigkill for just that command?

My answer:

You can set FinalKillSignal= to a different signal than SIGKILL, e.g. SIGCONT.

Note that systemd is going to get very annoyed at being unable to stop the service, but it should keep running.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.