0

I have successfully created a service in systemd that runs a script just before server shutdown/reboot. But the problem is that my script takes too long to execute (around 3-4 minutes) and the server shuts down before it is finished. Debug log indicates that the script is indeed running but it is stopped halfway.

Here are my two different ways of implementing this service:

[Unit]
Description=Stop Service

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/home/user/stop-service.sh

[Install]
WantedBy=multi-user.target

and

[Unit]
Description=Stop Service
DefaultDependencies=no
Before=final.target


[Service]
ExecStart=/home/user/stop-service.sh

[Install]
WantedBy=final.target

They both successfully call stop-service.sh but the script takes too long to run. Is there any way to make sure stop-service.sh finishes running before server is fully shut down?

My server is RHEL7.

1
  • 1
    It may be useful to add the relevant section of the log in your question
    – Zanna
    Commented Jul 26, 2020 at 14:08

1 Answer 1

3

I found a way that works for me:

[Unit]
Description=Stop Service

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/home/user/stop-service.sh
TimeoutStopSec=300

[Install]
WantedBy=multi-user.target

According to my research, the systemd service has a default 90 seconds wait time for each service to run their stop command (ExecStop) before sending the terminal signal. TimeoutStopSec=300 will set the wait time to 5 minutes for that one particular service.

If you want to change the default wait time, you can locate the file /etc/systemd/system.conf and modify the value of DefaultTimeoutStopSec, then reload the daemon with

systemctl daemon-reload

to load the new value.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .