1

I've been chasing my tail on this without success and I can't find why my script doesn't run on shutdown...

Running Ubuntu 20.04 (Desktop).

I created a /usr/local/sbin/run_on_shutdown.sh:

#!/bin/sh
REBOOT=$( systemctl list-jobs | egrep -q 'reboot.target.*start' && echo "rebooting" || echo "not_rebooting" )
if [ $REBOOT = "not_rebooting" ]; then
########################################################################
# Put Bash commands here to be executed on shutdown but not on reboot. #
# For example, backup home directories to an external USB HDD. #
########################################################################

curl --silent --show-error -X POST https://example.com/trigger_something/abcdefkey
fi

I've chmod +x /usr/local/sbin/run_on_shutdown.sh

And created a systemd service:

[Unit]
Description=Run run_on_shutdown.sh at shutdown
DefaultDependencies=no
Requires=network.target network-online.target
Before=network-online.target shutdown.target halt.target

# If your script requires any mounted directories, add them below:
# RequiresMountsFor=/home

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/run_on_shutdown.sh

[Install]
WantedBy=multi-user.target halt.target shutdown.target

Set permissions with chmod 644 /etc/systemd/system/run_on_shutdown.service

Reloaded the daemon: sudo systemctl daemon-reload

And enabled it with: sudo systemctl enable run_on_shutdown.service

But... it doesn't run on shutdown... but runs with sudo systemctl enable run_on_shutdown.service --now

I can't find any evidence of it being called on shutdown anywhere so...

Can somebody help me or point me in the right direction please?

Thanks!

6
  • 1
    I'm uncertain, but I think you are missing Conflicts=shutdown.target and RemainAfterExit=yes. This will keep your service active and stop your service when shutdown.target starts (ExecStart becomes ExecStop instead).
    – user986805
    Feb 1, 2021 at 20:30
  • don't think Require= is necessary either.
    – user986805
    Feb 1, 2021 at 20:32
  • Thanks, editing and testing right now, I'll post the results later!
    – MaldonadoR
    Feb 2, 2021 at 0:09
  • Added 'Conflicts=shutdown.target' and 'RemainAfterExit=yes', comented out 'Requires=network.target network-online.target' but no dice. Is there any way I can "see" what is the shutdown process running or error messages?
    – MaldonadoR
    Feb 2, 2021 at 9:11
  • have you seen How to run a script with systemd right before shutdown? (especially the last part) think it may be a better solution than a .service.
    – user986805
    Feb 2, 2021 at 9:57

1 Answer 1

0

This will create the file shutdown.log in /opt/tmp on shutdown.target but not on reboot.target

[Unit]
Description=My Shutdown Service
DefaultDependencies=no
Before=shutdown.target reboot.target
Conflicts=reboot.target

[Service]
ExecStart=/bin/bash \
    -c 'mkdir -p /opt/tmp && \
        echo shutdown | ts >> $_/shutdown.log'
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=shutdown.target
4
  • Remember to systemctl daemon-reload, systemctl enable my-shutdown.service
    – user986805
    Feb 2, 2021 at 12:41
  • This did it! I just replaced 'mkdir -p /opt/tmp && echo shutdown | ts >> $_/shutdown.log' with my curl command! Thank you both very much for the help!
    – MaldonadoR
    Feb 2, 2021 at 13:47
  • If you intend to use the network, you may need to add dependencies to the network.target (...and you should not need this REBOOT and if statement).
    – user986805
    Feb 2, 2021 at 13:56
  • Slightly older Ubuntu 18.04LTS doesn't have "Before"
    – Vorsprung
    Feb 9, 2023 at 17:06

You must log in to answer this question.

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