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