Table of Contents
I hope you are already familiar with systemd and the basic difference between SysV and systemd. Earlier I had written an article with examples to create your own systemd service unit file. Now let's see how to run script with systemd at shutdown only. This script should not be called at reboot as ideally every script also gets called during reboot stage.
As a system administrator there may be a situation wherein you would want to run script at shutdown only and not at reboot. Such as performing some backup or any other task using a script or command at shutdown. We can also call a command using these steps to be called only at shutdown in Linux.
I have used CentOS/RHEL 7/8 Linux node to verify these steps to run script at shutdown only (not at reboot).
Some more articles on similar topic:
- How to execute a command or script with systemd at shutdown only and not at reboot in Linux
- How to execute a command or script with systemd right before login prompt appears on terminal in Linux
- How to execute a command or script at system startup using systemd without using cronjob in Linux
- How to execute a command or script after N minutes of boot up (system startup) with systemd in Linux
- How to halt system reboot or shutdown and read user input during boot up stage in Linux
- How to execute a command or script using systemd right before shutdown happens in Linux
- How to run a service as a specific user or group using systemd in Linux
Step 1: Create a sample script
Now to run script at shutdown only with systemd we need a script. Below is a dummy script which will help us recognise if our script is getting called at
systemctl list-jobs we know the currently active and running target. So the script should only work at
[root@centos-8 ~]# cat /tmp/script.sh #!/bin/bash # Run script with systemd at shutdown only case $1 in start) systemctl list-jobs | egrep -q 'reboot.target.*start' && echo "starting reboot" >> /tmp/file systemctl list-jobs | egrep -q 'shutdown.target.*start' && echo "starting shutdown" >> /tmp/file ;; stop) systemctl list-jobs | egrep -q 'reboot.target.*start' || echo "stopping" >> /tmp/file ;; esac
I have also written a stop function but that anyhow will not be called but just to prove this fact this function is required. In such case only
ExecStart will be called to run script with systemd at shutdown only and not at reboot
Step 2: Create unit file to run script with systemd at shutdown only
I have already written another article with the steps and examples to create a systemd service unit file. Here we will create our systemd unit file under
/etc/systemd/system by the name
Below is our systemd unit file which we will use to run script with systemd at shutdown only (not at reboot)
[root@centos-8 ~]# cat /etc/systemd/system/myscript.service [Unit] Description=Run my custom task at shutdown only DefaultDependencies=no Conflicts=reboot.target Before=poweroff.target halt.target shutdown.target Requires=poweroff.target [Service] Type=oneshot ExecStart=/tmp/script.sh start RemainAfterExit=yes [Install] WantedBy=shutdown.target
- Conflicts If a unit has a
Conflicts=setting on another unit, starting the former will stop the latter and vice versa
- Before= They configure ordering dependencies between units.
- Requires Configures requirement dependencies on other units. If this unit gets activated, the units listed here will be activated as well.
- Type Configures the process start-up type for this service unit. One of simple, forking, oneshot, dbus, notify or idle.
- ExecStart Commands with their arguments that are executed when this service is started.
- RemainAfterExit Takes a boolean value that specifies whether the service shall be considered active even when all its processes exited. Defaults to no.
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 myscript Created symlink /etc/systemd/system/poweroff.target.wants/myscript.service → /etc/systemd/system/myscript.service. Created symlink /etc/systemd/system/halt.target.wants/myscript.service → /etc/systemd/system/myscript.service.
Step 3: Verify the systemd unit file configuration
Let us verify our systemd unit file. We will perform a shutdown of my CentOS/RHEL 7/8 Linux node to check if it run script with systemd at shutdown only and not at reboot.
[root@centos-8 ~]# shutdown now
After the node comes UP we check the content of our /tmp/file where we were writing our content from the dummy script
login as: root firstname.lastname@example.org's password: Last login: Tue Jan 14 22:41:22 2020 from 10.0.2.2 [root@centos-8 ~]# cat /tmp/file starting shutdown
As expected the script was called at shutdown. Now let us do a reboot and check the same.
Before rebooting I will clean the content of /tmp/file
[root@centos-8 ~]# cat /tmp/file [root@centos-8 ~]#
So as expected again the content of
/tmp/file is empty so this script was not called at reboot.
Lastly I hope the steps from the article to run script with systemd at shutdown only and not at reboot on CentOS/RHEL 7/8 Linux was helpful. So, let me know your suggestions and feedback using the comment section.
How can I create a systemd service unit in CentOS/RHEL 7/8 that executes something only at shutdown time and not at reboot