Many sysadmins encounter contradictory instructions when opening their cron files. The default /var/spool/cron/crontabs/root
file typically contains these conflicting lines:
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (several lines later...)
# Edit this file to introduce tasks to be run by cron.
While the crontab -e
command is the recommended approach, manually editing cron files is technically possible if you:
- Maintain proper file permissions (600 for user crontabs)
- Use atomic writes to prevent corruption
- Understand the cron syntax thoroughly
Consider these scenarios where direct editing might be preferable:
# Bulk editing multiple cron jobs
vim /var/spool/cron/crontabs/username
# Programmatic modification (use with caution!)
sed -i '/oldjob/d' /var/spool/cron/crontabs/username
echo "0 3 * * * /path/to/newjob.sh" >> /var/spool/cron/crontabs/username
If you choose manual editing, always:
- Backup the original file first
- Verify syntax with
crontab -l
after editing - Check system logs for cron errors
For most users, crontab -e
remains the safer choice because it:
- Validates syntax before saving
- Handles file locking properly
- Maintains correct permissions automatically
Advanced users who need direct file access should implement these safety checks:
#!/bin/bash
# Safe edit wrapper for cron files
CRON_FILE="/var/spool/cron/crontabs/$USER"
BACKUP="/tmp/cron_backup_$(date +%s)"
cp "$CRON_FILE" "$BACKUP"
vim "$CRON_FILE" || { echo "Edit failed, restoring backup"; cp "$BACKUP" "$CRON_FILE"; exit 1; }
if ! crontab -l >/dev/null 2>&1; then
echo "Invalid syntax detected, restoring backup"
cp "$BACKUP" "$CRON_FILE"
exit 1
fi
When working with cron jobs, administrators often face the question: should we use crontab -e
or manually edit files like /var/spool/cron/crontabs/root
? The conflicting comments in default cron files don't help:
# DO NOT EDIT THIS FILE - edit the master and reinstall. # (minutes) (hours) (day of month) (month) (day of week) (command) # Edit this file to introduce tasks to be run by cron.
Direct file editing can cause several issues:
- Potential file corruption if the editor doesn't handle file locking properly
- Syntax errors that might not be validated
- Permission problems if the file attributes change
- No automatic backup like
crontab -e
provides
There are legitimate cases for direct editing:
# Bulk updates - much faster than multiple crontab -e commands # Example: mass update of paths in existing cron jobs sed -i 's|/old/path|/new/path|g' /var/spool/cron/crontabs/username # Scripted deployments where crontab -e isn't practical cat </var/spool/cron/crontabs/deployuser # Maintenance jobs 0 3 * * * /usr/bin/backup.sh EOF
If you must edit directly:
- Always create a backup first:
cp /var/spool/cron/crontabs/user /backup/user.cron.$(date +%F)
- Use
crontab -l
to verify after editing - Check file permissions remain correct (usually 600)
- Validate syntax with
crontab -u user -l
Consider these safer methods:
# For system-wide cron jobs (preferred on many distros) sudo nano /etc/crontab # Using configuration management tools # Ansible example: - name: Ensure cron job exists cron: name: "daily backup" hour: 2 job: "/opt/backup.sh"