News

🐧 Linux Tutorials

Free Linux tutorials and step-by-step guides for developers.

How to Set Cron Schedules on Linux

Cron is the standard task scheduler on Linux and Unix-like systems. It lets you run commands or scripts automatically — hourly, daily, weekly, or on any custom schedule — without any manual intervention.


Prerequisites

  • A Linux system (Debian, Ubuntu, RHEL, CentOS, Arch, etc.)
  • Terminal access
  • Basic command-line familiarity

Step 1 — Open Your Crontab

Each user has their own crontab file. Open it with:

crontab -e

This opens the file in your default editor (usually nano or vi). If it is your first time, the file will be empty or contain only comments.

To view your current crontab without editing:

crontab -l

To edit another user's crontab (requires root):

sudo crontab -e -u username

Step 2 — Understand the Syntax

Each line in a crontab defines one scheduled job:

MIN  HOUR  DOM  MON  DOW  COMMAND
Field Meaning Allowed values
MIN Minute 0–59
HOUR Hour of the day 0–23 (0 = midnight)
DOM Day of the month 1–31
MON Month 1–12
DOW Day of the week 0–6 (0 = Sunday, 6 = Saturday)

Use * to mean "every value". Use */n for "every n units". Use a-b for a range. Use a,b,c for a list.


Step 3 — Write Your First Job

Add a line at the bottom of the file. For example, to run a backup script every day at 2:00 AM:

0 2 * * * /usr/local/bin/backup.sh

To run a health check every 15 minutes:

*/15 * * * * /usr/local/bin/health-check.sh >> /var/log/health.log 2>&1

The >> /var/log/health.log 2>&1 part appends both standard output and errors to a log file — highly recommended so you can inspect what happened.


Step 4 — Common Schedule Examples

Schedule Expression
Every minute * * * * *
Every hour (at :00) 0 * * * *
Every day at midnight 0 0 * * *
Every day at 9:00 AM 0 9 * * *
Weekdays at 9:00 AM 0 9 * * 1-5
Every 15 minutes */15 * * * *
Every 6 hours 0 */6 * * *
Every Sunday at midnight 0 0 * * 0
First day of every month 0 0 1 * *

Step 5 — Use Special Shortcuts

Instead of five fields, cron supports readable shortcuts:

Shortcut Equivalent
@reboot Once at system startup
@hourly 0 * * * *
@daily 0 0 * * *
@midnight 0 0 * * *
@weekly 0 0 * * 0
@monthly 0 0 1 * *
@yearly 0 0 1 1 *

Example — run a script once after every reboot:

@reboot /usr/local/bin/startup-init.sh

Step 6 — Save and Verify

After editing, save and exit the editor (in nano: Ctrl+O, then Ctrl+X). Cron will automatically install the new schedule. Verify it is active:

crontab -l

Check the system cron log to confirm jobs are running:

# Debian/Ubuntu
grep CRON /var/log/syslog | tail -20

# RHEL/CentOS
grep CRON /var/log/cron | tail -20

# systemd-based systems
journalctl -u cron --since "1 hour ago"

Step 7 — Set Environment Variables (Optional)

Crontab does not inherit your shell environment. If your script relies on PATH or other variables, set them at the top of the crontab:

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAILTO=admin@example.com

0 2 * * * /usr/local/bin/backup.sh

Setting MAILTO causes cron to email you the output of each job. Set it to an empty string (MAILTO="") to suppress emails entirely.


Step 8 — System-Wide Cron Jobs

For jobs that run regardless of which user is logged in, use the system crontab files:

  • /etc/crontab — system-wide crontab with an extra USER field
  • /etc/cron.d/ — drop-in job files, same format as /etc/crontab
  • /etc/cron.hourly/, /etc/cron.daily/, /etc/cron.weekly/, /etc/cron.monthly/ — place executable scripts here, no schedule syntax required

System crontab format (note the extra USER column):

# /etc/crontab
MIN  HOUR  DOM  MON  DOW  USER  COMMAND
0    2     *    *    *    root  /usr/local/bin/backup.sh

Troubleshooting

Job does not run

  • Check crontab -l to confirm the entry is saved.
  • Use absolute paths for both the command and any files it references — cron's PATH is minimal.
  • Redirect output to a log file to capture errors.
  • Verify cron daemon is running: systemctl status cron (Debian/Ubuntu) or systemctl status crond (RHEL/CentOS).

Job runs but produces no output or errors

  • Add >> /tmp/myjob.log 2>&1 to the end of the command and inspect /tmp/myjob.log.

Script works in terminal but not in cron

  • Source your profile inside the script: source /etc/profile or set the required environment variables at the top of the crontab.

Next Steps

Use the Cron Visual Editor on SysEmperor to build, test, and document your cron schedules visually — paste an existing crontab to inspect it, or use the builder to construct new entries with a live human-readable preview.