🥾Delayed Reboot Script for Debian Controlled by systemd 👟

Ronald BartelsRonald Bartels
2 min read

To create a systemd service that runs /usr/local/sbin/custom-reboot.sh once, exactly 3 minutes after boot, follow these steps:


Step 1: Create the Systemd Service File

Open a terminal and create a new systemd service file:

sudo nano /etc/systemd/system/custom-reboot.service

Add the following content:

[Unit]
Description=Run Custom Reboot Script Once After Boot
After=network.target

[Service]
Type=oneshot
ExecStart=/bin/bash /usr/local/sbin/custom-reboot.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
  • Type=oneshot: Ensures the script runs once.

  • RemainAfterExit=yes: Prevents systemd from considering the service as inactive immediately.

  • After=network.target: Ensures the network is up before running the script.

  • ExecStart=/bin/bash /usr/local/sbin/custom-reboot.sh: Runs the script.


Step 2: Create a Timer to Delay Execution

Now, create a timer that runs the service 3 minutes after boot:

sudo nano /etc/systemd/system/custom-reboot.timer

Add the following content:

[Unit]
Description=Run Custom Reboot Script 3 Minutes After Boot
After=network.target

[Timer]
OnBootSec=3min
Unit=custom-reboot.service

[Install]
WantedBy=timers.target
  • OnBootSec=3min: Runs the service 3 minutes after boot.

  • Unit=custom-reboot.service: Links the timer to the service.


Step 3: Reload Systemd and Enable the Timer

Reload systemd and enable the timer:

sudo systemctl daemon-reload
sudo systemctl enable custom-reboot.timer
sudo systemctl start custom-reboot.timer

To verify that the timer is active:

systemctl list-timers --all | grep custom-reboot

Step 4: Test the Setup

To test the setup manually without rebooting:

sudo systemctl start custom-reboot.service

To check the logs:

journalctl -u custom-reboot.service --no-pager --since "5 minutes ago"

Step 5: Ensure the Script is Executable

Make sure /usr/local/sbin/custom-reboot.sh exists and is executable:

sudo chmod +x /usr/local/sbin/custom-reboot.sh

Wrap

✅ Now, your custom script will automatically run once, exactly 3 minutes after boot! 🚀

10
Subscribe to my newsletter

Read articles from Ronald Bartels directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ronald Bartels
Ronald Bartels

Driving SD-WAN Adoption in South Africa