How to Recover Deleted Crontab Jobs and Append New Entries Safely


2 views

When you execute crontab filename.txt, it completely replaces your existing crontab with the contents of the file - this catches many admins by surprise. Unlike crontab -e which opens your current crontab for editing, the file import method is destructive by design.

Here are several approaches to recover lost cron jobs:

1. Check System Logs

Some Linux distributions log cron activity:

grep CRON /var/log/syslog
journalctl -u cron.service --since "2 hours ago"

2. Check Backup Locations

Many systems automatically backup crontabs:

ls /var/spool/cron/crontabs/
ls /var/backups/

3. Reconstruct from Source Control

If your deployment uses configuration management:

git log -p path/to/cron/files
ansible-doc -l | grep cron

To safely add new jobs without overwriting:

(crontab -l ; echo "*/5 * * * * /path/to/script.sh") | crontab -

Or for multiple entries:

crontab -l > current_cron
echo "0 3 * * * /backup.sh" >> current_cron
echo "*/10 * * * * /monitor.sh" >> current_cron
crontab current_cron
rm current_cron
  • Always run crontab -l before making changes
  • Keep cron configuration in version control
  • Use configuration management tools for production
  • Create regular backups of /var/spool/cron/

Here's how I recently recovered lost production cron jobs:

# 1. Check for automatic backups
sudo ls -la /var/backups/cron*

# 2. Found yesterday's backup
sudo cp /var/backups/cron.bak ~/cron_recovery

# 3. Add new job to recovered file
echo "*/15 * * * * /opt/app/healthcheck.sh" >> ~/cron_recovery

# 4. Verify contents
cat ~/cron_recovery

# 5. Install recovered crontab
crontab ~/cron_recovery

html

When you run crontab filename, it doesn't append to existing cron jobs - it completely replaces your current crontab with the contents of the file. This is a common pitfall that even experienced sysadmins occasionally stumble into.

While there's no guaranteed way to recover overwritten crontab entries, here are several approaches to try:

1. Check System Backups

Most production environments have regular backups. Look for:

/var/backups/cron*
/var/spool/cron/crontabs/*
/etc/cron.*/

2. Search Bash History

The original crontab commands might be in your shell history:

history | grep crontab
# Or check the history file directly:
cat ~/.bash_history | grep crontab

3. Examine System Logs

Cron activity might be logged in:

/var/log/syslog
/var/log/cron
journalctl -u cron.service

Always Dump Current Crontab First

Before making changes:

crontab -l > crontab.backup
# Then edit:
crontab -e

Use Version Control

Store your crontab in Git:

crontab -l > ~/dotfiles/crontab
cd ~/dotfiles && git commit -am "Update crontab"

Implementation Example

Here's how to safely add a new cron job:

# Step 1: Backup current
crontab -l > current_cron.txt

# Step 2: Edit
echo "0 3 * * * /path/to/backup.sh" >> current_cron.txt

# Step 3: Verify
cat current_cron.txt

# Step 4: Apply
crontab current_cron.txt

For production environments, consider infrastructure-as-code:

- name: Ensure cron jobs are configured
  ansible.builtin.cron:
    name: "Daily backup"
    minute: "0"
    hour: "3"
    job: "/path/to/backup.sh"