How to Migrate Cron Jobs and Scheduled Tasks
Cron jobs and scheduled tasks are easy to overlook during a server migration, yet they often handle critical functions like backups, log rotation, report generation, and application maintenance. Missing a cron job can lead to data loss or degraded functionality. This guide ensures every scheduled task is captured and recreated on your new Breeze.
Step 1: Inventory All Cron Jobs on the Source Server
Cron jobs can be defined in multiple locations. Check each one systematically:
User Crontabs
List cron entries for each user on the source server:
# List current user's cron
crontab -l
# List cron for a specific user
sudo crontab -u www-data -l
sudo crontab -u root -l
Export each user's crontab to a file:
crontab -l > /tmp/crontab-$(whoami).txt
sudo crontab -u www-data -l > /tmp/crontab-www-data.txt
sudo crontab -u root -l > /tmp/crontab-root.txt
System Cron Directories
Check the system-wide cron directories for scripts and drop-in files:
ls -la /etc/cron.d/
ls -la /etc/cron.daily/
ls -la /etc/cron.hourly/
ls -la /etc/cron.weekly/
ls -la /etc/cron.monthly/
Also check /etc/crontab for any entries defined there:
cat /etc/crontab
Systemd Timers
Modern Linux systems may use systemd timers instead of or alongside cron. List all active timers:
systemctl list-timers --all
For each custom timer, note the timer unit and its associated service unit.
Step 2: Document Each Job
For each scheduled task, document:
- The schedule expression (e.g.,
*/5 * * * *) - The command or script that runs
- The user context it runs under
- Any environment variables it depends on
- File paths it reads from or writes to
- Dependencies on other services (database, network, etc.)
Step 3: Transfer Scripts to the New Breeze
Copy all cron-related scripts to the new Breeze:
rsync -avz user@source-ip:/opt/scripts/ /opt/scripts/
rsync -avz user@source-ip:/etc/cron.d/ /etc/cron.d/
rsync -avz user@source-ip:/etc/cron.daily/ /etc/cron.daily/
rsync -avz user@source-ip:/etc/cron.hourly/ /etc/cron.hourly/
Verify that all scripts have execute permissions:
chmod +x /opt/scripts/*.sh
Step 4: Recreate User Crontabs
Import the saved crontabs on the new Breeze:
crontab /tmp/crontab-$(whoami).txt
sudo crontab -u www-data /tmp/crontab-www-data.txt
sudo crontab -u root /tmp/crontab-root.txt
Review each entry and update any file paths or hostnames that have changed in the migration.
Step 5: Recreate Systemd Timers
If you use systemd timers, copy both the .timer and .service unit files:
rsync -avz user@source-ip:/etc/systemd/system/backup.timer /etc/systemd/system/
rsync -avz user@source-ip:/etc/systemd/system/backup.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
Step 6: Test Each Job
Manually run each scheduled task to verify it works in the new environment:
# Test a cron command directly
/opt/scripts/backup.sh
# Test a systemd timer's service
sudo systemctl start backup.service
sudo systemctl status backup.service
Check the output and log files to ensure each task completes successfully. Monitor /var/log/syslog or journalctl for cron execution logs on the new Breeze.