Prerequisites
Before setting up timers, make sure you have:
- SSH access to your VPS
- Root or sudo privileges
💡 Systemd timers offer better logging, dependency handling, and integration than traditional cron.
Step 1: Create a Service File (The Task to Run)
Connect to your VPS:
ssh hxroot@YOUR_SERVER_IP -p 22
sudo nano /etc/systemd/system/backup-task.service
Add:
[Unit]
Description=Daily Backup Service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
User=root
StandardOutput=journal
Type=oneshot indicates the service runs once and exits.
Step 2: Create a Timer File (When to Run)
sudo nano /etc/systemd/system/backup-task.timer
Add:
[Unit]
Description=Run backup daily at 2:00 AM
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
Step 3: Enable and Start the Timer
sudo systemctl daemon-reload
sudo systemctl enable backup-task.timer
sudo systemctl start backup-task.timer
Step 4: Verify Timer Status
sudo systemctl list-timers backup-task.timer
Example output:
NEXT LEFT LAST PASSED UNIT ACTIVATES
Tue 2026-04-30 02:00:00 UTC 16h left Mon 2026-04-29 02:00:00 UTC 10h ago backup-task.timer backup-task.service
Common OnCalendar Expressions
hourly– Run every hour at minute 0daily– Run daily at 00:00:00weekly– Run weekly on Monday at 00:00:00monthly– Run on first day of month at 00:00:00*-*-* 02:00:00– Every day at 2 AMMon..Fri 09:00:00– Weekdays at 9 AMSun 03:00:00– Sundays at 3 AM*-*-01 00:00:00– First day of each month
Advanced Timer Options
[Timer]
OnBootSec=10min
OnUnitActiveSec=24h
RandomizedDelaySec=5min
AccuracySec=1h
- OnBootSec – Run after boot
- OnUnitActiveSec – Run time after last activation
- RandomizedDelaySec – Adds random delay to prevent thundering herd
- AccuracySec – Allowed scheduling inaccuracy (for efficiency)
Test Timer Without Waiting
sudo systemctl start backup-task.service
Manually runs the service to test.
Disable a Timer
sudo systemctl disable backup-task.timer
sudo systemctl stop backup-task.timer
View All Timers
systemctl list-timers --all
Check Timer Logs
journalctl -u backup-task.service
✅ Systemd timer configured. It runs tasks on schedule with better integration than cron.