Skip to main content

Linux Pre-shutdown and Post-bootup Task Automation

There are always plenty to do before a machine is shutdown and after a machine is startup. With systemd adopted by most linux distributions, it is possible to automate these tasks in parallel.

We can write various shell scripts to automate the tasks and specify them with different systemd units.

We may ask our service to run after some specific tasks with After= option and some interesting services are listed as following:

Name Service or Target Name
Networking network.target, network-online.target
Firewall firewalld.service
Container containerd.service, dockerd.service
Ceph RBD Mapper rbdmap.service
Printer printer.target and cups.service
Samba Share smbd.service
Virtualization qemu-kvm.service, libvirtd.service

Mount point required can be specified with RequiresMountsFor= option:

RequiresMountsFor=/path/to/A /path/to/B /path/to/C

Preparation

For pre-shutdown tasks, we need to create a directory at /lib/systemd/system/pre-shutdown.service.d/ and put the following script at

/lib/systemd/system/[email protected]

[Unit]
Description=Pre-shutdown Tasks

[Service]
Type=oneshot
RemainAfterExit=false
ExecStart=/bin/bash /lib/systemd/system/pre-shutdown.service.d/%i

[Install]
WantedBy=multi-user.target

For post-bootup tasks, we need to create a directory at /lib/systemd/system/post-bootup.service.d/ and put the following script at

/lib/systemd/system/[email protected]

[Unit]
Description=Post-bootup Tasks
After=network.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/bin/bash /lib/systemd/system/post-bootup.service.d/%i

[Install]
WantedBy=multi-user.target

Ceph Example

/lib/systemd/system/pre-shutdown.service.d/ceph

#!/bin/bash
ceph osd set noout
ceph osd set norebalance
ceph osd set norecover

/lib/systemd/system/post-bootup.service.d/ceph

#!/bin/bash
ceph osd unset noout
ceph osd unset norebalance
ceph osd unset norecover

After creating these files, we can enable them via:

sudo systemctl enable pre-shutdown@ceph
sudo systemctl enable post-bootup@ceph

Libvirt Example

/lib/systemd/system/pre-shutdown.service.d/libvirt

#!/bin/bash
export LIBVIRT_MACHINES=($(virsh list | tail +3 | awk '{ print $1 }'))
for machine in ${LIBVIRT_MACHINES[@]}; do
  virsh suspend "${machine}"
done

/lib/systemd/system/post-bootup.service.d/libvirt

#!/bin/bash
export LIBVIRT_MACHINES=($(virsh list | tail +3 | awk '{ print $1 }'))
for machine in ${LIBVIRT_MACHINES[@]}; do
  virsh resume "${machine}"
done

After creating these files, we can enable them via:

sudo systemctl enable pre-shutdown@libvirt
sudo systemctl enable post-bootup@libvirt

Send Email Example

Send an email to notify admin that the node is going down.

/lib/systemd/system/pre-shutdown.service.d/mail

#!/bin/bash
mail -s "Node Shutdown #$(hostname) @$(date +%Y-%m-%d-%H:%M:%S)" [email protected] < /dev/null

After creating the file, we can enable it via:

sudo systemctl enable pre-shutdown@mail

 

Comments

Comments powered by Disqus