1

I have a systemd service that works for what I want, which is to run a script when the system shuts down. However, this also runs on a reboot which is not what I want.

This is the service file:

[Unit]
Description=Shutdown script
Requires=network.target
Before=poweroff.target halt.target
DefaultDependencies=no

[Service]
Type=oneshot
TimeoutSec=5
RemainAfterExit=true
User=sysadm
Group=sysadm
ExecStart=/bin/true
ExecStop=/usr/bin/myscript.sh

[Install]
WantedBy=multi-user.target systemd-halt.service systemd-poweroff.service

Is there any way to make this work for shutdowns only?

1 Answer 1

1

I'm not sure if this is the most optimal solution, but it seems to do the trick.

I changed the ExecStart line as follows based on this answer:

ExecStop=/bin/bash -c 'systemctl list-jobs | grep -Eq 'reboot.target.*start' || /usr/bin/myscript.sh'

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.