In this article I will share example and sample systemd unit service file to run script with systemd right before shutdown in CentOS/RHEL 7/8 Linux. Using this systemd unit file you can run either some command or script as a last service before shutdown in Linux.
Now ideally any systemd service gets called via ExecStop at shutdown
stage and with ExecStart at boot up stage. But here our requirement is
little different, we wish to run script with systemd right before
shutdown stage. Now this can be a normal shutdown or reboot.
Step 1: Overview on systemd
If you are a beginner to systemd then I would recommend you to also readOverview on systemd and how it is different from legacy SysV scriptsbefore starting with this tutorial.
Step 2: Create Sample Script
Now to run script with systemd right before shutdown we need a script or
command. For the sake of this article I will use a script from another
article of mine /tmp/startup_script.sh. This script will run for 3
minutes in a loop to check and make sure the script is not killed
due to shutdown. We want the script to run and halt the shutdown until
completely executed
# cat /tmp/startup_script.sh
#!/bin/bash
z=0
for i in {1..3}; do
sleep 1m
((z++))
echo ""
done
Provide executable permission to the script
chmod u+x /tmp/startup_script.sh
Step 3: Create unit file to run script with systemd right before shutdown
Now as highlighted under step 1, I have already written another article
with the steps to create a new systemd
unit file. Here we will name our systemd unit file as
run-before-shutdown.service under /etc/systemd/system. Below is the
content of run-before-login-prompt.service
[root@centos-8 ~]# cat /etc/systemd/system/run-before-shutdown.service
[Unit]
Description=Run my custom task at shutdown
DefaultDependencies=no
Before=shutdown.target
[Service]
Type=oneshot
ExecStart=/tmp/startup_script.sh
TimeoutStartSec=0
[Install]
WantedBy=shutdown.target
Here the main task is done by Before=shutdown.target and
TimeoutStartSec=0. For more details check man page of
systemd.service
TimeoutStartSec= When a service doesn't signal start-up completion within TimeoutStartSec, systemd considers the service failed;
for long-running shell scripts it is essential to modify TimeoutStartSec or disable the timeout logic altogether
as above, with TimeoutStartSec=0. See man systemd.service for more details.
Before= If the script needs to be run before other services--for example, prior to starting sshd or console/graphical
logins ensure there is a Before=XYZ.service in the [Unit] section and a corresponding RequiredBy=XYZ.service
in the [Install] section.
Refresh the systemd configuration files
[root@centos-8 ~]# systemctl daemon-reload
Enable the script to automatically start at next boot
[root@centos-8 ~]# systemctl enable run-before-shutdown.service
Created symlink /etc/systemd/system/shutdown.target.wants/run-before-shutdown.service → /etc/systemd/system/run-before-shutdown.service.
Step 3: Verify the systemd unit file configuration
Now since we are done with the setting up of systemd. Let us verify our configuration.
reboot

We are using Oracle VirtualBox so post reboot we will check the console of our Virtual Machine
As expected we see that our systemd service is running and not allowing shutdown to complete.
Lastly I hope the steps from the article to run script with systemd right before shutdown on CentOS/RHEL 7/8 Linux was helpful. So, let me know your suggestions and feedback using the comment section.
Related Searches: run script with systemd right before shutdown. how to execute shell script before shutdown. run script before shutdown linux. run script on shutdown linux


