DEV Community


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/                                                                                                    
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
Description=Run my custom task at shutdown only

ExecStart=/tmp/ start

Enter fullscreen mode Exit fullscreen mode

This example is taken from here.

This is a pretty neat way to run shutdown scripts.

Top comments (0)

Advice For Junior Developers

Advice from a career of 15+ years for new and beginner developers just getting started on their journey.