Skip to main content

Systemd Scripts

Systemd can be used to run scripts as services. This is useful for running scripts that need to be run periodically, or that need to be run on a schedule.

Here is an example of how to create a script that will run a script every 5 minutes.

  1. Create a new file called 5min.sh in your home directory.

    #!/bin/bash

    while true
    do
    // some work goes here
    sleep 5m
    done
  2. Make the file executable

    chmod +x 5min.sh
  3. Create a systemd service file

    sudo nano /lib/systemd/system/shellscript.service 
  4. Add the following to the file:

    [Unit]
    Description=Every 5 minutes

    [Service]
    ExecStart=~/5min.sh

    [Install]
    WantedBy=multi-user.target
  5. Reload systemd

    systemctl daemon-reload
  6. Start the service

    systemctl start 5min.service
  7. Verify the service is running

    systemctl status 5min.service
  8. Stop the service

    systemctl stop 5min.service
  9. Enable to start on boot

    systemctl enable 5min.service
  10. Check to see if the service is enabled

    systemctl is-enabled 5min.service
  11. Start the service

    systemctl start 5min.service

Note: If you want to disable the service from starting on boot, use systemctl disable 5min.service