How to run script with systemd at shutdown only (not at reboot)


systemd, How To

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

 

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 reboot.target or shutdown.target. With systemctl list-jobs we know the currently active and running target. So the script should only work at shutdown.target or reboot.target

[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 myscript.service

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

Here from the man page of systemd.unit and systemd.service

  • 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

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.

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
root@127.0.0.1'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.

Related Searches: How can I create a systemd service unit in CentOS/RHEL 7/8 that executes something only at shutdown time and not at reboot

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

5 thoughts on “How to run script with systemd at shutdown only (not at reboot)”

  1. tested on centos8. it need to change
    “ExecStart=/tmp/script.sh start”
    in
    “ExecStart=/usr/bin/bash /tmp/script.sh start”
    and, naturally, “/bin/bash” in “/usr/bin/bash”

    Reply
  2. Hi all,
    I tried to run the service before shutdown using the above steps but while booting it shows that service is not activated.

    Reply
    • Hello,
      The provided steps are well tested but if you are facing any issue then please provide all the relevant details.
      Your environment? script? service? logs? your requirement ?

      Reply

Leave a Comment