I have a script that I want to run just before shutdown on my CentOS 7 machine. A process to do so using systemd is well-described in this previous question. Basically, I just drop the script I want to execute into
/usr/lib/systemd/system-shutdown/ and it will run automatically.
This works, but my script can take a while (several minutes). It seems that there is a 90-second timeout, after which my script gets killed and the system proceeds to halt. How can I change this time limit?
Based on the discussion here, I tried setting
/etc/systemd/system.conf, but it didn't have the desired effect.
I dug a little more and found the reason why: when shutdown scripts are run this way, they are executed by the
systemd-shutdown binary. Its source code can be found here. Specifically, this piece of code is responsible for executing the scripts:
execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
DEFAULT_TIMEOUT_USEC is defined in
basic/def.h as follows:
#define DEFAULT_TIMEOUT_USEC (90*USEC_PER_SEC)
So there appears to be a hard-coded 90-second limit, making this mechanism useless for my need here. What other method would be appropriate to run a script at shutdown? Could I create my own systemd service that is
WantedBy the halt target? I would like to run the script once the filesystems have been remounted readonly (so pretty late in the process).