The cron utility in Linux uses a specific syntax with five time fields followed by the command to execute:
* * * * * command-to-execute ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ │ │ │ │ └───── day of week (0 - 6) (0 is Sunday) │ │ │ └────────── month (1 - 12) │ │ └─────────────── day of month (1 - 31) │ └──────────────────── hour (0 - 23) └───────────────────────── min (0 - 59)
To execute a script precisely at 3:00 PM every day, use this crontab entry:
0 15 * * * /path/to/your/script.sh
Breakdown:
- 0
- Minute (0 means top of the hour)
- 15
- Hour in 24-hour format (15 = 3 PM)
- *
- Every day of month
- *
- Every month
- *
- Every day of week
Here are some common scenarios with their crontab entries:
# Run backup script at 3 PM daily 0 15 * * * /usr/local/bin/backup-script.sh # Execute PHP script with output logging 0 15 * * * /usr/bin/php /var/www/cron/daily_report.php >> /var/log/cron.log 2>&1 # Python script with virtual environment 0 15 * * * /home/user/venv/bin/python /home/user/scripts/daily_cleanup.py
Environment Variables: Cron jobs run with a minimal environment. Always use absolute paths and set required variables:
0 15 * * * . /home/user/.profile; /path/to/script.sh
Logging Output: Always capture output for debugging:
0 15 * * * /path/to/script.sh > /var/log/script.log 2>&1
Testing Your Crontab: Verify your syntax before implementation:
crontab -l # List current jobs crontab -e # Edit cron jobs
- Permission Issues: Ensure your script is executable (
chmod +x script.sh
) - Path Problems: Use absolute paths for all files and commands
- Time Zone Confusion: Cron uses system timezone - verify with
date
command - Email Alerts: Cron sends output to local mail - redirect or handle appropriately
Cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals. The crontab (cron table) file is where these schedules are defined.
The crontab syntax consists of five fields followed by the command to be executed:
* * * * * command_to_execute - - - - - | | | | | | | | | +----- Day of the week (0 - 6) (Sunday=0) | | | +------- Month (1 - 12) | | +--------- Day of the month (1 - 31) | +----------- Hour (0 - 23) +------------- Minute (0 - 59)
To run a script every day at 3 PM, you would use the following crontab entry:
0 15 * * * /path/to/your/script.sh
Breaking this down:
- 0
- Minute (0 means the start of the hour)
- 15
- Hour in 24-hour format (3 PM)
- *
- Every day of the month
- *
- Every month
- *
- Every day of the week
Let's say you have a backup script at /home/user/backup.sh
that you want to run daily at 3 PM. Here's how you would set it up:
First, open the crontab editor:
crontab -e
Then add the following line:
0 15 * * * /home/user/backup.sh
Ensure your script has execute permissions:
chmod +x /home/user/backup.sh
You can verify your cron job is set up correctly by listing your current cron jobs:
crontab -l
To test if it's working, you can temporarily set it to run in a few minutes and check the logs:
*/5 * * * * /home/user/backup.sh >> /var/log/backup.log 2>&1
It's good practice to redirect output to a log file:
0 15 * * * /home/user/backup.sh >> /var/log/backup.log 2>&1
- Ensure the script has the correct shebang (e.g., #!/bin/bash
)
- Use absolute paths in your script
- Consider environment variables - cron runs with a minimal environment
- Check mail (or your specified log file) for error messages
If you specifically need to run at midnight, you can use:
@daily /path/to/script.sh
However, this doesn't allow specifying a particular hour like our 3 PM example.