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