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 in your home directory.


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

    chmod +x
  3. Create a systemd service file

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

    Description=Every 5 minutes


  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