Skip to main content
RunBook Academy

← All labs in Linux

Lab · intermediate · ~45 min

Lab: Replace a cron job with a systemd timer

B · Nested virtualisationC · Simulation

Objectives

  • Create a systemd service
  • Create a systemd timer
  • Replace a cron job
  • Add locking and alerting

Prerequisites

This lab converts a cron job to a systemd timer. By the end you will have the discipline for the migration.

Tasks

Task 1: Identify a cron job to migrate

crontab -l

Pick a job. For this lab, use a simple one:

*/5 * * * * /usr/local/bin/health-check.sh

Task 2: Capture the job

sudo cp /usr/local/bin/health-check.sh /usr/local/bin/health-check.sh.bak

# Add locking
cat /usr/local/bin/health-check.sh.bak | sudo tee /usr/local/bin/health-check.sh

# Make sure it\'s the same; no change
sudo chmod +x /usr/local/bin/health-check.sh

Task 3: Create the service

sudo tee /etc/systemd/system/health-check.service <<EOF
[Unit]
Description=Health check every 5 minutes

[Service]
Type=oneshot
ExecStart=/usr/local/bin/health-check.sh
EOF

Task 4: Create the timer

sudo tee /etc/systemd/system/health-check.timer <<EOF
[Unit]
Description=Run health check every 5 minutes

[Timer]
OnCalendar=*:0/5
Persistent=true
Unit=health-check.service

[Install]
WantedBy=timers.target
EOF

Task 5: Enable and test

sudo systemctl daemon-reload
sudo systemctl enable --now health-check.timer

# Verify
sudo systemctl list-timers --all | grep health-check

# Trigger manually
sudo systemctl start health-check.service
sudo systemctl status health-check.service
sudo journalctl -u health-check.service

Task 6: Remove the cron job

crontab -e
# Remove the */5 * * * * line

Task 7: Document

CRON TO TIMER MIGRATION
========================
Job: /usr/local/bin/health-check.sh
Schedule: */5 * * * *

Old: cron entry
New: /etc/systemd/system/health-check.{service,timer}

Verification:
- Timer enabled and active
- Service runs on schedule
- Output captured in journald
- Cron entry removed

Deliverables

  • · A working systemd service
  • · A working systemd timer
  • · The replaced cron job removed

Verification status

Last reviewed
2026-08-09
Executed end to end
not yet run on hardware

The commands and configuration here have been reviewed against the verified software versions, but nobody has run this lab start to finish on a system meeting its prerequisites. Treat the Expected Outcome as the intended result rather than an observed one, and keep the Cleanup section to hand.