2

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 DefaultTimeoutStartSec=infinity and DefaultTimeoutStopSec=infinity in /etc/systemd/system.conf, but it didn't have the desired effect.

Edit: 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).

Jason R
  • 645
  • Running systemctl show SERVICE_NAME.service -p TimeoutStopUSec you could see the timeout set by systemd to service.

    Instead of editing the package's service file in /usr/lib/systemd/system/ which will get overridden on package upgrade, use: sudo EDITOR=/bin/vi systemctl edit <service-name> This will safely edit the file /etc/systemd/system/service-name.service.d/override.conf.

    This file need only contain: [Service] TimeoutStopSec=5000 # or whatever value you want

    – Valeriy Solovyov Dec 07 '20 at 22:57

0 Answers0