Crontab: Scheduling Cron Jobs in Linux

By 

Updated on

8 min read

Crontab cron job scheduling syntax and examples in Linux

Cron is a scheduling daemon that executes tasks at specified intervals. These tasks are called cron jobs and are most commonly used to automate system maintenance, run backups, and trigger recurring scripts.

For example, you could set a cron job to automate repetitive tasks such as backing up databases , updating the system with the latest security patches, checking disk space usage , or sending emails.

Cron jobs can be scheduled to run by minute, hour, day of the month, month, day of the week, or any combination of these.

Quick Reference

ScheduleCron Expression
Every minute* * * * *
Every 5 minutes*/5 * * * *
Every 15 minutes*/15 * * * *
Every hour0 * * * *
Every day at midnight0 0 * * *
Every day at 9 AM0 9 * * *
Weekdays at 9 AM0 9 * * 1-5
Every Sunday at midnight0 0 * * 0
Every month on the 1st0 0 1 * *
Every year on Jan 1st0 0 1 1 *
At system boot@reboot

What Is a Crontab File

Crontab (cron table) is a text file that specifies the schedule of cron jobs. There are two types of crontab files: system-wide crontab files and individual user crontab files.

User crontab files are named after the user and their location varies by distribution. On Red Hat, Fedora, and Derivatives, crontab files are stored in the /var/spool/cron directory. On Debian, Ubuntu, and Derivatives, they are stored in /var/spool/cron/crontabs.

Although you can edit user crontab files manually, it is recommended to use the crontab command.

The /etc/crontab file and the scripts inside the /etc/cron.d directory are system-wide crontab files that can only be edited by system administrators.

In most Linux distributions you can also place scripts inside the /etc/cron.{hourly,daily,weekly,monthly} directories and the scripts will be executed at the corresponding interval.

Crontab Syntax and Operators

Each line in a user crontab file contains six fields separated by a space, followed by the command to run:

txt
* * * * * command(s)
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

The first five fields may contain one or more values separated by a comma, or a range of values separated by a hyphen.

  • * — The asterisk means any value. An asterisk in the Hour field means the task runs every hour.
  • , — The comma allows you to specify a list of values. For example, 1,3,5 in the Hour field runs the task at 1 am, 3 am, and 5 am.
  • - — The hyphen specifies a range of values. For example, 1-5 in the Day of week field runs the task every weekday (Monday through Friday).
  • / — The slash specifies a step value. For example, */4 in the Hour field runs the task every four hours, equivalent to 0,4,8,12,16,20. You can also use a range: 1-30/10 is the same as 1,11,21.

System-wide Crontab Files

The syntax of system-wide crontab files differs slightly from user crontabs. It contains an additional mandatory user field that specifies which user will run the cron job:

txt
* * * * * <username> command(s)

Predefined Macros

There are several special cron schedule macros used to specify common intervals. You can use these shortcuts in place of the five-column date specification:

  • @yearly (or @annually) — Run once a year at midnight on January 1st. Equivalent to 0 0 1 1 *.
  • @monthly — Run once a month at midnight on the first day of the month. Equivalent to 0 0 1 * *.
  • @weekly — Run once a week at midnight on Sunday. Equivalent to 0 0 * * 0.
  • @daily — Run once a day at midnight. Equivalent to 0 0 * * *.
  • @hourly — Run once an hour at the start of the hour. Equivalent to 0 * * * *.
  • @reboot — Run once at system startup.

crontab Command

The crontab command allows you to install, view , or open a crontab file for editing:

  • crontab -e — Edit the crontab file, or create one if it does not already exist.
  • crontab -l — Display the crontab file contents.
  • crontab -r — Remove your current crontab file.
  • crontab -i — Remove your current crontab file with a confirmation prompt before removal.
  • crontab -u <username> — Edit another user’s crontab file. Requires administrator privileges.

The crontab command opens the file using the editor specified by the VISUAL or EDITOR environment variables.

Crontab Variables

The cron daemon automatically sets several environment variables :

  • The default PATH is set to /usr/bin:/bin. If the command you are running is not in this path, use the absolute path to the binary or set a custom PATH at the top of your crontab. You cannot implicitly append to $PATH as you would in a regular script.
  • The default shell is /bin/sh. To use a different shell, set the SHELL variable at the top of your crontab.
  • Cron runs commands from the user’s home directory. Override this with the HOME variable.
  • Output is emailed to the crontab owner by default. Set MAILTO=email@example.com to redirect notifications, or set MAILTO="" to disable email entirely.

Crontab Restrictions

The /etc/cron.deny and /etc/cron.allow files allow you to control which users have access to the crontab command. Each file contains a list of usernames, one per line.

By default, only the /etc/cron.deny file exists and is empty, which means all users can use the crontab command. To deny a specific user, add their username to this file.

If /etc/cron.allow exists, only users listed in that file can use the crontab command.

If neither file exists, only users with administrative privileges can use crontab.

Cron Job Examples

The following examples show how to schedule tasks at different intervals:

If you are creating your first cron job, use this safe workflow:

  1. Open your crontab with crontab -e.
  2. Add one simple test job such as */5 * * * * date >> /tmp/cron-test.log.
  3. Save the file and confirm it is installed with crontab -l.
  4. Wait a few minutes and verify /tmp/cron-test.log is updated.
  • Run a command at 15:00 on every weekday (Monday through Friday):

    txt
    0 15 * * 1-5 command
  • Run a script every 5 minutes and redirect standard output to /dev/null so only errors are emailed:

    txt
    MAILTO=email@example.com
    */5 * * * * /path/to/script.sh > /dev/null
  • Run two commands every Monday at 3 PM:

    txt
    0 15 * * Mon command1 && command2
  • Run a PHP script every 2 minutes and append the output to a log file :

    txt
    */2 * * * * /usr/bin/php /path/to/script.php >> /var/log/script.log
  • Run a script every day, every hour on the hour, from 8 AM through 4 PM:

    txt
    0 08-16 * * * /path/to/script.sh
  • Run a script on the first Monday of each month at 7 AM:

    txt
    0 7 1-7 * 1 /path/to/script.sh
  • Run a script at 9:15 PM on the 1st and 15th of every month:

    txt
    15 21 1,15 * * /path/to/script.sh
  • Set custom environment variables and run a command every minute:

    txt
    HOME=/opt
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    SHELL=/bin/bash
    MAILTO=email@example.com
    * * * * * command

Troubleshooting

Cron job does not run
Verify the syntax with crontab -l and test the command manually from the shell first. Also confirm that the cron daemon is running with systemctl status cron (Debian/Ubuntu) or systemctl status crond (Red Hat/Fedora).

Command works in the shell but not in cron
Cron uses a minimal PATH (/usr/bin:/bin). Use the absolute path to your binary (for example, /usr/bin/python3 instead of python3), or set a full PATH at the top of your crontab.

Script runs but produces no output or email
Cron sends output by email, which requires a working mail transfer agent. To capture output reliably, redirect it to a file: command >> /var/log/myjob.log 2>&1. To redirect both stdout and stderr, see the bash-redirect-stderr-stdout guide.

Jobs run at the wrong time
Cron uses the system timezone . Verify it with timedatectl and adjust your schedule accordingly, or set a TZ variable at the top of your crontab.

Permission denied when running a script
Make the script executable with chmod +x /path/to/script.sh and ensure the cron user has read and execute permission on the file.

FAQ

How do I edit my crontab?
Run crontab -e. This opens your user crontab in the default editor. Save and exit to install the new schedule. Changes take effect immediately.

What is the difference between a user crontab and /etc/crontab?
User crontabs (managed with crontab -e) run jobs as that user and contain five time fields plus the command. The /etc/crontab and files in /etc/cron.d/ are system-wide, contain an additional username field, and can only be edited by root.

How do I redirect cron job output to a file?
Append >> /path/to/logfile.log 2>&1 to your cron command. The 2>&1 part redirects stderr to stdout so both are captured in the same file.

How do I run a cron job at system startup?
Use the @reboot macro instead of a time expression: @reboot /path/to/script.sh. The job runs once each time the system boots.

Conclusion

Cron is the standard tool for automating recurring tasks on Linux. Understanding the five-field time syntax, operators, and environment variables gives you full control over when and how your jobs run. For listing and managing existing jobs, see the crontab list guide .

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page