There is a lot of solution here to execute a script at shutdown/reboot, but I want my script to only execute at shutdown.

I've tried to put my script in /usr/lib/systemd/systemd-shutdown, and check the $1 parameter, as seen here, but it doesn't work.

Any ideas ?

system : archlinux with gnome-shell

$systemctl --version                                                                                                                                                                                 
systemd 229
  • I don't think this will be the solution, but you should put custom scripts into /etc/systemd/system/. The /usr/lib/systemd/system/ directory is meant to be used for system-provided scripts.
    – ILMostro_7
    Commented May 21, 2016 at 9:27

4 Answers 4


I've finally found how to do that.

It's a bit hackish thought, but it works.

I've used some part of this thread : https://stackoverflow.com/questions/25166085/how-can-a-systemd-controlled-service-distinguish-between-shutdown-and-reboot

and this thread : How to run a script with systemd right before shutdown?

I've created this service /etc/systemd/system/shutdown_screen.service

Description=runs only upon shutdown

ExecStop=/bin/bash /usr/local/bin/shutdown_screen


Which will be executed at shudown/reboot/halt/whatever. (don't forget to enable it)

And in my script /usr/local/bin/shutdown_screen I put the following :

# send a shutdown message only at shutdown (not at reboot)    
/usr/bin/systemctl list-jobs | egrep -q 'reboot.target.*start' || echo "shutdown" | nc 4243 -w 1

Which will send a shutdown message to my arduino, whom will shutdown my screen.


According to the systemd.special man-page, you should use Before=poweroff.target.


A special target unit for shutting down and powering off the system.

Applications wanting to power off the system should start this unit.

runlevel0.target is an alias for this target unit, for compatibility with SysV.

Additionally, as I mentioned in my comment, you should put custom scripts into /etc/systemd/system/. The /usr/lib/systemd/system/ directory is meant to be used for system-provided scripts.

So, maybe something like this:

Description=runs only upon shutdown

  • Well, it doesn't seems to work. I've edited your .service like this (otherwise, symlink aren't created) http://pastebin.com/dq01rQVM But nothing is executed at shutdown. I forgot to said that I need a working connection for my script. But I've tried with a echo "test" > /root and nothing was created.
    – benoit2600
    Commented May 21, 2016 at 12:42

From reading the answers here there seems to be a lot misunderstanding about how systemd works. Firstly don't use conflicts to exclude a target. It is ment to keep to conflicting services from being run at the same time.

If a unit has a Conflicts= setting on another unit, starting the former will stop the latter and vice versa.

Unit means a .service file ment to launch perticular service not a target to be reach. In other-words Conflicts=reboot.target is meaningless at best and at worst it will prevent you from rebooting. Don't do this. It does not mean don't run this on reboot. It means abort either this service or the reboot.target depending on timing and how systemd interprets this erroneous use of conflicts.

Here is example of a currently setup unit (aka .service file) that runs only on shutdown not reboot:

Description=Play sound
Before=poweroff.target halt.target


WantedBy=poweroff.target halt.target 

poweroff.target is equivalent to the old systemv run level0 which is only reached at shutdown. halt.target is an alternate shutdown path used by systemd also not reachable by reboot. The install section tells systemd to add this service to the list that must be completed before poweroff.target or halt.target will be considered reached.

This service is installed and running on my system.

  • For me this did not worked. It ran both on reboot and halt on a raspbian. Also I could not make it worked before the network is down. Commented Aug 23, 2019 at 14:33
  • For anyone else trying to run a unit before the network is down you need to use a different target in before. The above example is not guaranteed to do so. Commented Nov 8, 2019 at 16:42

I have tested most of the post in here and various sites. The only one that actually worked to run only at reboot (not on boot again - but actually failed when shutdown-halt-poweroff) is the post of @benoit2600 . his second/solution post

But after testing I found that you can use in Unit all 4 target and will run when one of them actually used ONLY. Rest as per his and your needs In "ExecStop=mycommand" I just typed my direct command (as I had no need for a full bash)

Conflicts=reboot.target poweroff.target halt.target shutdown.target

You must log in to answer this question.

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