Guide
How to Use Cron
Learn how to create cron jobs, understand cron syntax, verify your schedules, and debug common cron problems on Linux servers.
What Is Cron?
Cron is a Linux scheduler that runs commands automatically at specific times or intervals. You can use cron jobs for backups, reports, cleanup scripts, emails, imports, and other recurring tasks.
1. Open Crontab
Open your user crontab with this command:
crontab -eThe first time you run it, Linux may ask you to choose an editor. If you are unsure, choose nano.
2. Understand Cron Format
A standard cron job uses five schedule fields followed by the command:
* * * * * command_to_execute
| | | | |
| | | | day of week (0-7, Sunday = 0 or 7)
| | | month (1-12)
| | day of month (1-31)
| hour (0-23)
minute (0-59)3. Add Cron Job Examples
* * * * * /path/to/script.sh30 2 * * * /path/to/script.sh0 17 * * 0 /path/to/script.sh*/5 * * * * /path/to/script.sh4. Make Your Script Executable
If your cron job runs a script, make sure the script has execute permission:
chmod +x /path/to/script.sh5. Save and Exit
If you are using nano, save and exit with:
CTRL + X, then Y, then Enter6. Verify Cron Jobs
List installed cron jobs with:
crontab -l7. Check Cron Logs
On many Linux systems, cron logs can be checked from syslog:
grep CRON /var/log/syslogSome distributions use different log locations, such as /var/log/cron. Your hosting provider may also expose cron logs in the control panel.
Common Cron Mistakes
- Using the wrong file path
- Forgetting to make a script executable
- Relying on environment variables that cron does not load
- Using relative paths instead of full paths
- Forgetting that cron usually runs in the server timezone
Best Practice: Use Full Paths
Cron runs with a minimal environment, so full paths are safer:
/usr/bin/php /var/www/html/script.phpPro Tip: Log Output and Errors
Redirect both output and errors to a log file:
* * * * * /path/to/script.sh >> /var/log/myscript.log 2>&1Useful Commands for script.sh
A cron job often points to a shell script such as script.sh. Put your real automation commands inside that script and keep the crontab line simple.
Basic script.sh Template
#!/bin/bash
# Always use full paths in cron scripts
echo "Cron job started at $(date)"
/usr/bin/php /var/www/html/script.php
echo "Cron job finished at $(date)"Make the script executable:
chmod +x /path/to/script.shFile Operations
cp /source/file.txt /backup/file.txt
mv /old/file.txt /new/location/
rm /tmp/oldfile.txt
mkdir -p /var/backups/dailyHTTP and API Calls
curl -X GET https://example.com/api
curl -X POST -d "key=value" https://example.com/api
# Download a file
wget https://example.com/file.zip -O /tmp/file.zipDatabase Backup with MySQL
Example with password inline:
/usr/bin/mysqldump -u user -p'password' dbname > /backup/db.sqlMore secure interactive-style command:
/usr/bin/mysqldump -u user -p dbname > /backup/db.sqlRun PHP, Python, or Node Scripts
/usr/bin/php /var/www/html/cron.php
/usr/bin/python3 /home/user/script.py
/usr/bin/node /home/user/app.jsZip and Backup Files
tar -czf /backup/site_$(date +\%F).tar.gz /var/www/htmlClean Up Old Files
This deletes files older than 7 days:
find /backup/ -type f -mtime +7 -deleteLogging Output
echo "Process started" >> /var/log/myscript.logSend Email Notification
echo "Backup completed" | mail -s "Cron Status" you@example.comProduction-Style Backup Script
#!/bin/bash
LOG_FILE="/var/log/backup.log"
DATE=$(date +%F)
echo "[$DATE] Backup started" >> $LOG_FILE
# Backup database
/usr/bin/mysqldump -u root -p'password' mydb > /backup/db_$DATE.sql
# Backup files
tar -czf /backup/files_$DATE.tar.gz /var/www/html
# Cleanup old backups
find /backup/ -type f -mtime +7 -delete
echo "[$DATE] Backup completed" >> $LOG_FILEImportant Script Tips
- Always use full paths, such as
/usr/bin/phpinstead ofphp. - Escape percent signs in crontab commands, for example
$(date +\%F). - Set a PATH manually if your script depends on system commands.
- Write logs for long-running or important jobs.
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binDebug Script for Failed Cron Jobs
Use debug mode and redirect all output to a log file:
#!/bin/bash
set -x
exec >> /var/log/debug.log 2>&1
echo "Starting job"