A simple timer setup for linux using systemd
For this example i will use "dl_backup" as my servic's name.
Steps:
Systemd files live in /etc/systemd/system/
- Create a service file
- Create a timer file
- Create a dummy file or script to run (edit on service file at ExecStart)
- Hook up the timer and service file with systemd. (Start / Enable)
dl_backup.service (file)
[Unit]
Description=Backup service for DefenceLogic backup scripts
Wants=dl_backup.timer
[Service]
#User=
Type=simple
WorkingDirectory=/home/dl/
ExecStart=/bin/bash -c 'cd /home/dl/ && ./test.sh '
[Install]
WantedBy=multi-user.target
dl_backup.timer (file)
[Unit]
Description=Run dl Backup daily
Requires=dl_backup.service
[Timer]
Unit=dl_backup.service
OnCalendar=*-*-* *:*:00 # runs every 1 min, change to your needs
Persistent=true
[Install]
WantedBy=timers.target
# Reload the files in the daemon /etc/systemd/system
sudo systemctl deamon-reload
# Start the service
sudo systemctl start dl_backup.service
# Start the timer for the service
sudo systemctl start dl_backup.service
# Load the new file
sudo systemctl daemon-reload
# Start your service
sudo systemctl start dl_backup.service
# To check the status of your service
sudo systemctl status dl_backup.service
sudo systemctl status dl_backup.timer
# To enable your service on every reboot
sudo systemctl enable dl_backup.service
# To disable your service on every reboot
sudo systemctl disable dl_backup.service
Watch service in real time
journalctl -S today -f -u dl_backup.service
Extra infos.
https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units
https://opensource.com/article/20/7/systemd-timers