Skip to content

Instantly share code, notes, and snippets.

@MohamedElashri
Created March 21, 2025 13:09
Show Gist options
  • Save MohamedElashri/c9b02cd07cfca169b7a81ddb85081c7d to your computer and use it in GitHub Desktop.
Save MohamedElashri/c9b02cd07cfca169b7a81ddb85081c7d to your computer and use it in GitHub Desktop.
Cron Jobs Cheat Sheet

Cron Jobs Cheat Sheet

1. Cron Time Format (Crontab Syntax)

Field Allowed Values Special Characters Meaning Example
Minute 0–59 * , - / Minute of the hour 0 = top of the hour
Hour 0–23 * , - / Hour of the day 5 = 5 AM
Day 1–31 * , - / Day of the month 15 = 15th
Month 1–12 or Jan–Dec * , - / Month of the year 1 = January
Weekday 0–6 or Sun–Sat * , - / Day of the week (0 = Sunday) 1 = Monday

Special Characters

  • * : Every value
  • , : Value list separator
  • - : Range of values
  • / : Step values (e.g. */5 means every 5th unit)

2. Common Schedule Examples

Expression Description Example Command
* * * * * Every minute * * * * * /path/to/script.sh
0 * * * * Every hour 0 * * * * /path/to/script.sh
0 0 * * * Every day at midnight 0 0 * * * /path/to/script.sh
0 9 * * 1-5 Weekdays at 9 AM 0 9 * * 1-5 /path/to/backup.sh
0 0 1 * * First day of every month 0 0 1 * * /path/to/monthly.sh
*/15 * * * * Every 15 minutes */15 * * * * /path/to/checker.sh
0 12 * * 0 Every Sunday at noon 0 12 * * 0 /path/to/clean.sh

3. Special Macros

Macro Equivalent Description
@reboot (N/A) Run at system startup
@yearly 0 0 1 1 * Once a year
@annually 0 0 1 1 * Once a year
@monthly 0 0 1 * * Once a month
@weekly 0 0 * * 0 Once a week
@daily 0 0 * * * Once a day
@midnight 0 0 * * * Once a day
@hourly 0 * * * * Once an hour

Example:

@reboot /path/to/startup_script.sh
@daily /usr/bin/freshclam

4. Crontab Commands

Command Description Example
crontab -e Edit current user’s crontab crontab -e
crontab -l List current user’s crontab crontab -l
crontab -r Remove current user’s crontab crontab -r
crontab -u <user> -e Edit another user’s crontab (as root) crontab -u root -e
crontab -u <user> -l List another user’s crontab (as root) crontab -u alice -l

5. Logging & Debugging

Method Description Example
Output redirection Save stdout and stderr /path/to/job.sh > /tmp/out.log 2>&1
Use logger Log to syslog logger "Job ran at $(date)"
Check cron log (if enabled) /var/log/cron or /var/log/syslog grep CRON /var/log/syslog

6. Environment Variables in Crontab

  • PATH, HOME, SHELL can be declared at the top
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

7. Good Practices

Practice Why
Use absolute paths Cron uses a limited environment
Use full paths for scripts/binaries Avoid relying on PATH
Log output for each job Easier debugging
Test scripts manually first Ensure correctness
Keep crontab clean and commented Maintenance & clarity

8. Example Full Crontab Entry

# Run backup script every day at 1am
0 1 * * * /home/melashri/scripts/backup.sh >> /home/melashri/logs/backup.log 2>&1

9. Edit system-wide cron jobs

File Purpose
/etc/crontab System-wide cron table
/etc/cron.d/ Drop-in files with cron jobs
/etc/cron.hourly/ etc. Periodic directories

10. Run Cron Job as a Specific User

In /etc/crontab or /etc/cron.d/* files:

# Format: m h dom mon dow user command
0 5 * * * root /usr/local/bin/system_maintenance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment