Administrating a Linux server often requires strict operational procedures. If you are preparing to power down a server for hardware maintenance or reboot it for a kernel upgrade, you rarely want to simply yank the metaphorical power cord. You might need to gracefully unmount network drives, execute a final database backup, or send a webhook notification to a Slack channel alerting your team that the server is going offline.
Instead of relying on human memory to manually execute these tasks before running the shutdown command, you can hook your custom bash scripts directly into the Linux shutdown sequence using systemd.
Why Systemd Over Older Methods?
Historically, Linux administrators placed shutdown scripts in the /etc/rc0.d/ directory (the old SysVinit method). However, nearly all modern Linux distributions (including Ubuntu, Debian, CentOS, and Fedora) now use systemd as their initialization system. Writing a custom systemd service is the most robust, reliable, and modern way to guarantee your script executes during the shutdown sequence.
Step 1: Prepare Your Script
First, ensure your script is ready and executable.
- Place your script in a safe, standard directory, such as
/usr/local/bin/.sudo nano /usr/local/bin/pre_shutdown_tasks.sh - Write your bash script. (Ensure the first line is
#!/bin/bash). - Make the script executable:
sudo chmod +x /usr/local/bin/pre_shutdown_tasks.sh
Step 2: Create the Systemd Service File
Now, we will create a service file that tells systemd exactly when and how to run your script.
- Navigate to the systemd directory and create a new service file:
sudo nano /etc/systemd/system/run-at-shutdown.service - Paste the following configuration block into the file:
[Unit] Description=Run custom script before server shutdown DefaultDependencies=no Before=shutdown.target ConditionPathExists=/usr/local/bin/pre_shutdown_tasks.sh [Service] Type=oneshot ExecStart=/usr/local/bin/pre_shutdown_tasks.sh TimeoutStartSec=0 [Install] WantedBy=shutdown.target - Save the file and exit the editor.
Understanding the Configuration
DefaultDependencies=no: By default, systemd assumes services want to run while the system is fully active. This flag tells systemd to ignore normal dependencies so the service can run during the volatile shutdown phase.Before=shutdown.target: This is the critical line. It explicitly commands systemd to execute this service before the actual shutdown target is reached.Type=oneshot: This ensures systemd waits for your script to completely finish executing before it proceeds with shutting down the rest of the system.TimeoutStartSec=0: Prevents systemd from killing your script if it takes longer than 90 seconds (useful for large backups).
Step 3: Enable the Service
Finally, we must reload systemd so it recognizes the new file, and then enable it so it becomes a permanent part of the boot/shutdown cycle.
sudo systemctl daemon-reload
sudo systemctl enable run-at-shutdown.service
Your automation is now active. The next time you type sudo reboot or sudo shutdown -h now, Linux will pause, execute your script completely, and only then proceed with gracefully powering down the machine.