Skip to main content

Cron and Crontab: The Practical Guide to Automating Tasks on Linux

In the world of system development and administration, the watchword is one: automation. Imagine having to perform a database backup every night, email a report every Monday morning, or clean up temporary files every hour. Performing these tasks manually is not only tedious, it is also a huge waste of time and a potential source of errors. This is where Cron comes in.

Cron is one of the most powerful and reliable utilities found in almost any Unix-like operating system (such as Linux). It is the "autopilot" that allows you to "schedule," i.e., schedule, the execution of commands or scripts at precise time intervals. In this handy guide, we'll show you everything you need to know to master cron and its inseparable companion, the crontab command.

Cron vs Crontab: Let's Get Clear

Before we begin, it is critical to clear up a common confusion. Although often used as synonyms, cron and crontab are not the same thing.

1. Cron: The Silent Automation Engine

Cron is a demon, which is a program that runs constantly in the background on your system. Its sole purpose is to check, minute by minute, a list of scheduled tasks and launch those for which the time has come. You never interact directly with the cron daemon; it does its work silently.

2. Crontab: Your Control Panel

Crontab (which stands for "Cron Table") is the command you use to interact with the cron daemon. Through crontab, you can view, create, edit or delete the list of tasks (called "cron jobs") to be performed by the cron daemon. Each user on the system can have their own personal crontab file.

The Crontab Syntax: Learning to Talk to Cron

The real power of cron lies in its syntax, which allows you to define with surgical precision when a command should be executed. A cron job is a single line of text with this structure:

* * * * * /path/path of/command to_execute

The initial five asterisks represent the unit of time and are at the heart of scheduling. Let's look at them in detail:

  • Minute (0 - 59)
  • Time (0 - 23)
  • Day of the Month (1 - 31)
  • Month (1 - 12)
  • Day of the Week (0 - 7, where both 0 and 7 represent Sunday)

Use of the asterisk *

An asterisk (*) in one position means "any". For example, * * * * means "every minute of every hour of every day...".

Here are some examples to clarify:

  • 0 2 * * * - Run every night at 02:00.
  • 0 9-18 * * 1-5 - Run every hour, from 9:00 am to 6:00 pm, Monday through Friday.
  • */15 * * * * - Run every 15 minutes.

Use of the comma ,

A comma (,) in one position means "and", understood as an "and" operator. For example, * 2,4 * * * means "Run every night at 02:00 and 4:00".

Here are some examples to clarify:

  • 0 2,4 * * * - Run every night at 02:00 and 4:00.
  • 0 9,18 * * 1-5 - Run every hour, at 9:00 am and 6:00 pm, Monday through Friday.
  • 11,41 * * * - Run every hour at minutes 11 and 41.

Use of the slash /

A slash (/) in one position means "every". For example, * */2 * * means "Run two hours, i.e. 00:00, 02:00, 4:00, and so on."

Here are some examples to clarify:

  • 0 */2 * * - Run every even-numbered day of the month.
  • 0 */12 * * 7 - Run every 12 hours on Sunday.
  • */30 * * * - Run every 30 minutes.

To test and validate your rules before you save them, we recommend using an online tool. On my favorites list:

Guide to Essential Crontab Commands

Interacting with crontab from the terminal is simple. The commands you will use 99% of the time are three.

crontab -e: Create and Edit Your Cron Jobs

This is the main command. It opens the current user's crontab file in a text editor (usually `nano` or `vi`). Here you can add, edit or remove your schedule lines.

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
* * * * user-name command to be executed

# Example: add this line to run a backup script every night at 3:30 a.m.
30 3 * * * * /usr/bin/php /var/www/html/backup.php

Once the file is saved and closed, the cron daemon will automatically load the new rules.

crontab -l: Display Active Jobs

This command is simply to show the contents of your crontab file, without opening it in edit mode. It is useful for a quick check.

$ crontab -l

# Output:
# 30 3 * * * * /usr/bin/php /var/www/html/backup.php

crontab -r: Remove All Jobs (Warning!)

Use this command with extreme caution! crontab -r instantly and without asking for confirmation removes the entire crontab file, deleting all your scheduled jobs. There is no recycle bin or easy way to undo the operation.

Practical Cron Job Examples

  1. Running a PHP script every 5 minutes:
     

    */5 * * * * /usr/bin/php /var/www/project/cron.php >/dev/null 2>&1

    Note: /dev/null 2>&1 is used to suppress the output of the command, preventing the system from sending you an email each time it runs.

  2. Empty a log folder every Sunday at 4 am:
     

    0 4 * * 0 rm -f /var/log/app_logs/*.log
  3. Launch a framework command (ex. Laravel Artisan) every hour:
     

    0 * * * * cd /var/www/laravel_app && php artisan schedule:run >> /dev/null 2>&1

Do you need Complex Automations?

These examples cover the basics, but the cron possibilities are endless. If you need to configure complex automation pipelines, manage execution permissions, or make sure your critical jobs always run error-free, you may need expert help.

Our Linux assistance service is designed to do just that. Let our experienced systems engineers handle your automations so you can focus on developing your business.

Frequently Asked Questions (FAQ) about Cron

1. Is there an alternative to cron for Windows?

Yes. The equivalent of cron in the Windows environment is called Scheduling Utility (Task Scheduler). It allows you to schedule the startup of programs or scripts with a graphical interface.

2. How can I see the output or errors of a cron job?

By default, cron sends the output of each command via email to the user who owns the crontab. For more robust logging, it is good practice to redirect the output to a specific log file. Example: * * * * * /my/command >> /var/log/my_cron.log 2>&1.

3. Why doesn't my cron job work?

The most common causes are:

  • Non-absolute paths: Cron does not have the same environment (PATH) as a logged in user. Always use absolute paths for commands (e.g., /usr/bin/php instead of php).
  • Permissions: Make sure that the script or file you are trying to execute has execution permissions (chmod +x_script.sh name).

4. How can I use a different editor for `crontab -e`?

You can set the environment variable EDITOR before running the command. For example, to use nano: export EDITOR=nano; crontab -e.

Add new comment

Comment

  • Allowed HTML tags: <br> <p> <code class="language-*"> <pre>
  • Lines and paragraphs break automatically.
  • Only images hosted on this site may be used in <img> tags.