I just want to run (as root) a command or script before each reboot/shutdown/poweroff on Ubuntu 21.04 (when I run the shutdown or poweroff commands, or using GNOME/KDE GUI options). I've tried putting the script inside /etc/init.d
and making symlinks to /etc/rc6.d/
and /etc/rc0.d/
but doesn't work.
Wonder if there's such a line for sudo crontab -e
.
You can do this by creating a service file, then reloading
systemd
. Here's how:Open Terminal (if it's not already open)
Ensure the script that you want to run at shutdown is executable:
Create a file in
/etc/systemd/system
for the shutdown service. For the sake of this example, I'll call my filenighty-night.service
.Add the following lines to the
.service
file, modifying it as needed:Restart the daemon:
That's all there is to it.