Automatically Shutdown Your Linux System 8 Hours After Booting with systemd Timer

If you want to automatically shut down your Linux machine, or specifically TrueNAS SCALE, 8 hours after booting, setting up a systemd timer is an effective way to do it. With a few simple steps, you can ensure that your system will power off after a set duration, making it ideal for automated processes or testing environments.

Step-by-Step Setup

Create the systemd service: First, create a systemd service that will shut down the system after 8 hours. You can do this by creating a service file at /etc/systemd/system/shutdown-timer.service with the following content:

    [Unit]
    Description=Shutdown 8 hours after boot
    After=multi-user.target
    
    [Service]
    Type=oneshot
    ExecStart=/sbin/shutdown -h +28800

    Create the timer: Then, create a systemd timer to initiate the service 1 minute after boot:

      [Unit]
      Description=Timer to shutdown system 8 hours after boot
      After=multi-user.target
      
      [Timer]
      OnBootSec=1min
      Unit=shutdown-timer.service
      
      [Install]
      WantedBy=timers.target

      Enable and start the timer: Run the following commands to reload the systemd configurations and start the timer:

      sudo systemctl daemon-reload
      sudo systemctl enable shutdown-timer.timer
      sudo systemctl start shutdown-timer.timer

      Manual Interrupt

      If you need to cancel the shutdown or modify the timer, you can do so manually. To cancel the scheduled shutdown, run:

      sudo shutdown -c

      To stop the timer from running in the future, use:

      sudo systemctl stop shutdown-timer.timer
      sudo systemctl disable shutdown-timer.timer

      Summary

      With this simple systemd timer setup, your Linux or TrueNAS SCALE system will automatically shut down 8 hours after boot. This can be useful for automated testing or saving power when the machine isn’t needed for extended periods. You can also manually cancel or adjust the timer as necessary, giving you full control over the system’s behavior.

      Similar Posts