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.
Create a new file called
5min.shin your home directory.#!/bin/bash
while true
do
// some work goes here
sleep 5m
doneMake the file executable
chmod +x 5min.shCreate a systemd service file
sudo nano /lib/systemd/system/shellscript.serviceAdd the following to the file:
[Unit]
Description=Every 5 minutes
[Service]
ExecStart=~/5min.sh
[Install]
WantedBy=multi-user.targetReload systemd
systemctl daemon-reloadStart the service
systemctl start 5min.serviceVerify the service is running
systemctl status 5min.serviceStop the service
systemctl stop 5min.serviceEnable to start on boot
systemctl enable 5min.serviceCheck to see if the service is enabled
systemctl is-enabled 5min.serviceStart the service
systemctl start 5min.service
Note: If you want to disable the service from starting on boot, use
systemctl disable 5min.service