Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

systemd service to start script at shutdown

Linux Kernel, Network, and Services configuration.
Post Reply
Message
Author
torstenv
Posts: 2
Joined: 2022-01-21 11:25

systemd service to start script at shutdown

#1 Post by torstenv »

Problem:
The execution of a script on shutdown is required (such as formerly scripts in rc0.d and rc6.d would) but the script is only being executed when the service is stopped manually using

Code: Select all

systemctl stop service
. It's not executed when performing a shutdown or reboot with

Code: Select all

shutdown -r now
as it's supposed to.

System: Debian 11

To reproduce:
install debian11 netinstall

Code: Select all

#!/bin/bash

cat << THISISTHEEND > /etc/systemd/system/sd.service
[Unit]
Description=do fancy stuff on shutdown
DefaultDependencies=no
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/runatshutdown.sh
TimeoutStartSec=0

[Install]
WantedBy=shutdown.target
THISISTHEEND

cat << THISISTHEEND > /usr/local/bin/runatshutdown.sh
#!/bin/bash
logger "hey, we're about to shut down this host"
touch /home/flag
THISISTHEEND

chmod u+x /usr/local/bin/startup_script.sh
systemctl enable sd
systemctl start sd
I also tried the following:

Code: Select all

[Unit]
Description=more details here
DefaultDependencies=no
After=final.target
Before=shutdown.target reboot.target halt.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/usr/local/bin/runatshutdown.sh

[Install]
WantedBy=multi-user.target

The first example is taken from this tutorial: https://www.golinuxcloud.com/run-script ... e_shutdown
The second example is what I've come up with using man pages and other documentation.

Help will be appreciated!

torstenv
Posts: 2
Joined: 2022-01-21 11:25

Re: systemd service to start script at shutdown [SOLVED]

#2 Post by torstenv »

SOLVED

Code: Select all

[Unit]
Description=Run my custom task at shutdown
DefaultDependencies=no
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/root/startup_script.sh
TimeoutStartSec=0

[Install]
WantedBy=shutdown.target

Post Reply