About Cron and Crontabs
Cron is a time-based job scheduler found in Unix-like operating systems (Linux, macOS, BSD).
It allows users to schedule commands, scripts, or programs to run automatically at specific times, dates, or intervals.
The name "cron" comes from "chronos," the Greek word for time.
What is a Crontab?
A crontab (cron table) is a configuration file that contains the schedule of cron entries to be run and at what times.
Each user on a system can have their own crontab file, and there are also system-wide crontab files for administrative tasks.
Where Cron is Available
Common Use Cases
- System Maintenance: Log rotation, cleanup tasks, system updates
- Data Backups: Automated database backups, file synchronization
- Monitoring: Health checks, performance monitoring, alert systems
- Data Processing: ETL jobs, report generation, batch processing
- Web Tasks: Cache clearing, sitemap generation, content publishing
- Development: Automated testing, deployment scripts, code quality checks
Basic Crontab Commands
# Edit your crontab
crontab -e
# List your cron jobs
crontab -l
# Remove your crontab
crontab -r
# Edit another user's crontab (requires privileges)
crontab -u username -e
Advanced Cron Concepts
System vs User Crontabs
There are two main types of crontab files:
- User crontabs: Individual users can create their own cron jobs using
crontab -e
- System crontabs: Located in
/etc/crontab and /etc/cron.d/, these require root access and have an additional "user" field
System Crontab Format
# System crontab format (has an additional user field)
# minute hour day month weekday user command
0 2 * * * root /usr/local/bin/backup.sh
Special Time Strings
Many cron implementations support special strings instead of the five-field format:
@reboot # Run once at startup
@yearly # Run once a year (0 0 1 1 *)
@annually # Same as @yearly
@monthly # Run once a month (0 0 1 * *)
@weekly # Run once a week (0 0 * * 0)
@daily # Run once a day (0 0 * * *)
@midnight # Same as @daily
@hourly # Run once an hour (0 * * * *)
Best Practices & Tips
Environment Setup
Set up your crontab environment properly at the top of your crontab file:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
MAILTO=admin@example.com
HOME=/home/username
This ensures your scripts run in a predictable environment with proper PATH and shell settings.
Logging and Debugging
Always redirect output to log files for debugging and monitoring:
# Log both stdout and stderr
0 2 * * * /path/to/backup.sh >> /var/log/backup.log 2>&1
# Separate logs for errors
0 2 * * * /path/to/backup.sh >> /var/log/backup.log 2>> /var/log/backup.error
# Suppress all output
0 2 * * * /path/to/script.sh > /dev/null 2>&1
Health Monitoring
Use monitoring services to track cron job execution:
# Using Healthchecks.io
0 5 * * * /backup.sh && curl -fsS -m 10 --retry 5 -o /dev/null https://hc-ping.com/your-uuid
# Using a simple log approach
0 5 * * * /backup.sh && echo "Backup completed at $(date)" >> /var/log/cron-health.log
Script Best Practices
Make your scripts cron-friendly:
#!/bin/bash
set -euo pipefail # Exit on error, undefined vars, pipe failures
# Use absolute paths
/usr/bin/php /var/www/script.php
# Lock files to prevent concurrent execution
LOCKFILE=/tmp/myscript.lock
if [ -f "$LOCKFILE" ]; then
echo "Script already running"
exit 1
fi
echo $$ > "$LOCKFILE"
trap "rm -f $LOCKFILE" EXIT
# Your script logic here
Testing Cron Jobs
Test your cron jobs before deploying:
# Test the command manually
/path/to/script.sh
# Test with the same environment as cron
env - /bin/bash -c '/path/to/script.sh'
# Check cron logs
tail -f /var/log/syslog | grep CRON
tail -f /var/log/cron # On some systems
# Test timing with a temporary entry
* * * * * /bin/echo "Test: $(date)" >> /tmp/crontest.log
Security Considerations
Keep your cron jobs secure:
- Use absolute paths to prevent PATH manipulation attacks
- Set restrictive permissions on scripts (chmod 700)
- Avoid putting sensitive data in cron commands
- Use dedicated service accounts for cron jobs when possible
- Regularly audit your crontab entries
# Good: uses absolute paths and proper permissions
0 2 * * * /usr/local/bin/backup.sh
# Bad: relies on PATH and has security risks
0 2 * * * backup.sh --password=secret123
Why We Built This Tool
At Beekeeper Studio, we spend a lot of time working on Linux and MacOS systems, which means we have to write the occasional crontab.
I wish I could remember Crontab syntax. I also wish I could remember `vi` keybindings. I can't remember either of them. So this little crontab generator is my way of maybe making the internet a little better.
Our crontab generator has no user tracking, no ad cookies, and our web analytics is even GDPR compliant without a cookie banner.
Maybe you'll see Beekeeper Studio in the future and think "those folks are cool" (our app has zero tracking too), maybe you'll try our app, maybe you won't. Either way, please enjoy our free cron tool! - Matthew, Beekeeper Studio founder