DEV Community

3 1

What is /usr/lib/systemd/system-shutdown ?

Everyone probably has heard of startup scripts such as Cloud-init or user-data which is passed as a startup script even libvirt has this feature.

Running a shutdown script is however not mentioned much, now why would you need a shutdown script, sometimes, a shutdown script is needed to do various things such as collect diagnosis etc.

The path /usr/lib/systemd/system-shutdown/ runs these scripts, however they are run quite late hence the system is mostly in read-only mode, for example to create a log file one would have to do

cat /usr/lib/systemd/system-shutdown/debug.sh                                                                                                    
#!/bin/sh
mount -o remount,rw /
dmesg > /home/leon/shutdown-log
mount -o remount,ro /
Enter fullscreen mode Exit fullscreen mode

The other way is to use a systemd script:

cat /etc/systemd/system/demo.service
[Unit]
Description=Run my custom task at shutdown only
DefaultDependencies=no
Conflicts=reboot.target
Before=poweroff.target halt.target shutdown.target
Requires=poweroff.target

[Service]
Type=oneshot
ExecStart=/tmp/script.sh start
RemainAfterExit=yes

[Install]
WantedBy=shutdown.target
Enter fullscreen mode Exit fullscreen mode

This example is taken from here.

This is a pretty neat way to run shutdown scripts.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay