cron jobs cheat sheet

Cron Job Overview

Cron jobs are scheduled tasks that run at fixed times, dates, or intervals on a Unix-based system. The cron daemon (crond) handles these tasks, which are specified in a file called the crontab.


Basic Cron Syntax

Cron jobs are scheduled in the crontab file with this format:

* * * * * 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)

Crontab Command

  • View current cron jobs: crontab -l
  • Edit cron jobs: crontab -e
  • Remove current cron jobs: crontab -r
  • Set user-specific cron jobs: crontab -u username -e

Fields Explained

FieldValues/Description
Minute0-59 (e.g., 5 means 5th minute of every hour)
Hour0-23 (e.g., 14 means 2 PM)
Day of month1-31 (e.g., 15 means the 15th day of the month)
Month1-12 (e.g., 6 means June)
Day of week0-6 (e.g., 0 is Sunday, 1 is Monday, etc.)

Special Characters

  • * : Any value (e.g., * * * * * runs the command every minute)
  • , : Separator for multiple values (e.g., 1,5,10 * * * * runs at minute 1, 5, and 10)
  • - : Range of values (e.g., 1-5 means days Monday to Friday)
  • / : Step values (e.g., */5 means every 5 minutes or every 5 days)

Examples of Cron Jobs

1. Run a command every minute

* * * * * /path/to/script.sh

2. Run a command at a specific time (e.g., at 2:30 AM every day)

30 2 * * * /path/to/script.sh

3. Run a command every Sunday at 4 AM

0 4 * * 0 /path/to/script.sh

4. Run a command on the 1st day of every month at midnight

0 0 1 * * /path/to/script.sh

5. Run a command every Monday to Friday at 6 PM

0 18 * * 1-5 /path/to/script.sh

6. Run a command every 5 minutes

*/5 * * * * /path/to/script.sh

7. Run a command every 10 minutes between 2 PM and 4 PM

*/10 14-16 * * * /path/to/script.sh

8. Run a command on the 15th day of every month at 9:15 PM

15 21 15 * * /path/to/script.sh

Redirect Output

  • Redirect output to a file: * * * * * /path/to/script.sh > /path/to/output.log 2>&1
    • > redirects stdout (standard output).
    • 2>&1 redirects stderr (standard error) to stdout.
  • Append output to a file: * * * * * /path/to/script.sh >> /path/to/output.log 2>&1

Cron Job User Permissions

  • System cron jobs are stored in /etc/crontab or /etc/cron.d/. These files can be modified by root or admins.
  • User-specific cron jobs are in /var/spool/cron/crontabs/username and can only be edited by the respective user or root.

System Cron Directories

  • /etc/crontab: Global system-wide cron jobs.
  • /etc/cron.d/: Directory for additional cron jobs by package maintainers.
  • /etc/cron.daily/: Directory for tasks to be run daily.
  • /etc/cron.hourly/: Directory for tasks to be run hourly.
  • /etc/cron.monthly/: Directory for tasks to be run monthly.
  • /etc/cron.weekly/: Directory for tasks to be run weekly.

Cron Expressions for Advanced Scheduling

You can also use cron expressions for advanced scheduling. Here are a few examples:

1. Every minute of every hour

* * * * *

2. Every 5 minutes

*/5 * * * *

3. Every day at midnight

0 0 * * *

4. Every Sunday at midnight

0 0 * * 0

5. Every weekday (Monday to Friday) at 9 AM

0 9 * * 1-5

Using Cron with Shell Scripts

To use cron with shell scripts:

  1. Make sure your script is executable: chmod +x /path/to/script.sh
  2. Use full paths in scripts (e.g., /usr/bin/python, /bin/bash), as cron uses minimal environments without paths set by the user.
  3. Use the proper shebang (#!/bin/bash) in your script to specify the shell.

Cron Logging

Cron logs its activity, which can help you debug failed jobs.

  • View cron logs (for Ubuntu/Debian): cat /var/log/syslog | grep CRON
  • View cron logs (for RedHat/CentOS): cat /var/log/cron

Tips

  • Cron environment: Cron jobs run in a very limited environment. It doesn’t load the user profile, so environment variables (like $PATH) might not be available. You may need to explicitly set these in your cron jobs. PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
  • Send email notifications: Cron will send output from commands to the user’s local mailbox by default. You can suppress this or redirect it using >/dev/null 2>&1.
  • Test your cron job: To avoid errors, test your cron commands directly in the terminal first.