Learn how to create your own .service files to run custom scripts or apps and automate tasks using systemd timers as a replacement for cron.
.service FileStore custom units in:
/etc/systemd/system/
Create a file like /etc/systemd/system/mytask.service:
[Unit]
Description=My Custom Script
After=network.target
[Service]
ExecStart=/usr/local/bin/myscript.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
โ Make sure the script is executable:
chmod +x /usr/local/bin/myscript.sh
sudo systemctl daemon-reload
sudo systemctl enable mytask.service
sudo systemctl start mytask.service
sudo systemctl status mytask.service
systemd TimerTimers schedule .service files like a cron job.
.timer and .service Filesmybackup.service[Unit]
Description=Run backup script
[Service]
Type=oneshot
ExecStart=/home/user/backup.sh
mybackup.timer[Unit]
Description=Daily Backup Timer
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
๐ Place both files in
/etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable mybackup.timer
sudo systemctl start mybackup.timer
| Value | Runs At |
|---|---|
OnBootSec=5min |
5 min after boot |
OnUnitActiveSec=1h |
1 hour after last run |
OnCalendar=daily |
Once daily |
OnCalendar=Mon *-*-* 06:00:00 |
Every Monday at 6 AM |
๐งช Test your timer:
sudo systemctl list-timers
| File Type | Purpose | Example Name |
|---|---|---|
.service |
What to run | myjob.service |
.timer |
When to run it | myjob.timer |
ExecStart |
Path to script or binary | /usr/bin/myscript.sh |
OnCalendar |
Timer schedule format | weekly, Mon 06:00 |
This guide covers two practical use cases using systemd:
Automatically start and restart your custom web server script if it crashes.
/etc/systemd/system/webapp.service[Unit]
Description=My Python Flask Web App
After=network.target
[Service]
ExecStart=/usr/bin/python3 /home/user/webapp/app.py
WorkingDirectory=/home/user/webapp
Restart=always
User=www-data
Environment=FLASK_ENV=production
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable webapp.service
sudo systemctl start webapp.service
sudo systemctl status webapp.service
โ App will restart on failure, auto-start on boot.
Automate apt update && upgrade every Sunday at 2 AM.
/etc/systemd/system/apt-update.service[Unit]
Description=Run weekly system updates
[Service]
Type=oneshot
ExecStart=/usr/bin/apt update && /usr/bin/apt upgrade -y
/etc/systemd/system/apt-update.timer[Unit]
Description=Weekly APT Update Timer
[Timer]
OnCalendar=Sun *-*-* 02:00:00
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable apt-update.timer
sudo systemctl start apt-update.timer
systemctl list-timers | grep apt-update
| Use Case | Type | File | Trigger |
|---|---|---|---|
| Auto-restart Flask app | .service |
webapp.service |
On boot / failure |
| Weekly APT updates | .service+.timer |
apt-update.timer |
Every Sunday @ 2 AM |
systemctl daemon-reload after editing unit files.journalctl -u service_name to check logs.Restart=always keeps services alive even after crashes.OnBootSec=5min for delayed starts after boot if needed.oneshot type for simple scripts.journalctl -u mytask.service to debug failures.systemctl start myjob.timer to activate manually.systemd lets you create reliable custom services and timers to automate tasks, replacing older tools like cron with more control and logging.