How to Use the cron Command to Schedule Automated Tasks in Linux

Managing a Linux server requires performing routine maintenance, such as backing up databases, rotating server logs, or running security scans. If you rely on a human administrator to log in via SSH and execute these scripts manually every night at 2:00 AM, mistakes will inevitably happen. To automate any script or command on a precise schedule, you must master the Linux cron daemon.

The cron daemon is a background service that wakes up every single minute, checks a text file (called a crontab) for any scheduled jobs, and executes them silently. In this guide, you will learn how to write cron expressions and schedule your automated tasks.

Accessing the Crontab

You do not edit the cron configuration files directly with a text editor. Instead, you use a dedicated command that ensures the syntax is checked before saving.

Open your terminal and type:

crontab -e

If this is your first time running the command, Linux will ask you to choose a default text editor (usually nano or vim). Once selected, the crontab file will open.

Understanding Cron Syntax

Every line in a crontab represents a single scheduled job. The syntax is strictly divided into six columns. The first five columns define when the job should run, and the sixth column defines what command to execute.

The five time columns are:

  1. Minute (0-59)
  2. Hour (0-23)
  3. Day of the Month (1-31)
  4. Month (1-12)
  5. Day of the Week (0-7, where both 0 and 7 represent Sunday)

An asterisk (*) means “every” or “always”.

Example 1: Running a Daily Backup

If you want a bash script named backup.sh to run every single day at exactly 2:30 AM, your cron entry would look like this:

30 2 * * * /home/user/scripts/backup.sh

This translates to: Minute 30, Hour 2, Every Day, Every Month, Every Day of the Week.

Example 2: Running a Job Every 15 Minutes

To run a task multiple times an hour (e.g., checking if a web server is still online), you use the division operator (/).

*/15 * * * * /usr/bin/php /var/www/check_status.php

This translates to: Every 15 minutes, Every Hour, Every Day, Every Month, Every Day of the Week.

Example 3: Running a Job on Specific Days

If you only want a report generated on Monday mornings at 9:00 AM, you use the Day of the Week column (Monday is 1).

0 9 * * 1 /home/user/scripts/report.sh

Crucial Rules for Cron Jobs

When administrators complain that a script works perfectly when they run it manually but fails when scheduled via cron, it is almost always due to one of two reasons:

  1. Absolute Paths: cron runs in a very restricted, minimal environment. It does not load your user profile’s $PATH variables. Therefore, you must use absolute paths for everything. Instead of typing python script.py, you must type the full path: /usr/bin/python3 /home/user/script.py.
  2. Executable Permissions: The file you are trying to run must have execute permissions. (Run chmod +x script.sh before scheduling it).

Once you add your line to the crontab -e window, simply save and exit the text editor. The cron daemon will immediately read the new file and begin executing your automated schedule.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.