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

Deepak Prasad

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).
Some more articles on similar topic:


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

[root@centos-8 ~]# cat /tmp/
# Run script with systemd at shutdown only 

case $1 in
        systemctl list-jobs | egrep -q '*start' && echo "starting reboot" >> /tmp/file
        systemctl list-jobs | egrep -q '*start' && echo "starting shutdown" >> /tmp/file

        systemctl list-jobs | egrep -q '*start' || echo "stopping"  >> /tmp/file


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
Description=Run my custom task at shutdown only

ExecStart=/tmp/ start


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

[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/ → /etc/systemd/system/myscript.service.
Created symlink /etc/systemd/system/ → /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
root@'s password:
Last login: Tue Jan 14 22:41:22 2020 from

[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

Views: 63

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 reach out to him on his LinkedIn profile or join on Facebook page.

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

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/ start”
    “ExecStart=/usr/bin/bash /tmp/ start”
    and, naturally, “/bin/bash” in “/usr/bin/bash”

  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.

    • 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 ?


Leave a Comment

GoLinuxCloud Logo

We try to offer easy-to-follow guides and tips on various topics such as Linux, Cloud Computing, Programming Languages, Ethical Hacking and much more.

Programming Languages