Avatar

Hey folks, I post some articles about technology and tricks & tips how to do some stuff in dev environment. πŸš€
My CV page is here.

How to Use crontab Like a Pro

5 minutes read

Table of Contents


image

If you ever needed to run a command automatically at a certain time on Linux, crontab is what you’re looking for. It’s simple once you get the pattern down.

Here’s everything you need to know to start β€” and actually understand what you’re doing.

What isΒ crontab? #

In its simplest form, crontab is a scheduler on Unix-based systems that automates tasks.

It executes commands or scripts at specific intervals, which you define in a crontab file. This is the file where cron jobs are set, and it’s the tool behind most of the scheduled processes you interact with daily.

You can think of cron as your personal assistant β€” once you set the schedule, cron takes over and makes sure everything happens on time, without further intervention from you. It runs tasks at the minute level (every 1 minute, every hour, every day, etc.), so it’s great for repetitive tasks.

Crontab Syntax Explained #

Every cron job follows a schedule format and is paired with a command to execute. The syntax might seem a bit tricky at first, but once you get the hang of it, it becomes second nature. Here’s the basic layout:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ minute (0–59)
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ hour (0–23)
β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of month (1–31)
β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ month (1–12)
β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of week (0–7) (Sunday = 0 or 7)
β”‚ β”‚ β”‚ β”‚ β”‚
β”‚ β”‚ β”‚ β”‚ β”‚
* * * * * command_to_execute

Explanation of Each Field:

  1. Minute (0-59) – When during the hour the command should run (e.g., 0 means at the start of the hour).
  2. Hour (0-23) – The hour at which the command should run (e.g., 14 means 2 PM).
  3. Day of Month (1-31) – The day of the month to execute the command (e.g., 15 for the 15th).
  4. Month (1-12) – The month of the year when the command should execute (e.g., 5 for May).
  5. Day of Week (0-7) – The day of the week (0 or 7 = Sunday, 1 = Monday, etc.).

Quick Examples #

Let’s break down some examples of crontab lines for clarity:

Crontab Line Meaning
0 0 * * * Run every day at midnight (00:00).
30 6 * * * Run every day at 6:30 AM.
0 */2 * * * Run every 2 hours.
0 0 1 * * Run once a month on the 1st day at midnight.
0 0 * * 0 Run every Sunday at midnight.

Special Crontab Macros #

Sometimes, you want to avoid remembering the exact numbers. Cron provides some convenient shortcuts you can use instead of the typical numbers. These are called macros, and they make your crontab much more readable:

Macro Equivalent Meaning
@reboot β€” Run once at startup (every reboot).
@yearly 0 0 1 1 * Run once a year (January 1st).
@annually 0 0 1 1 * Alias for @yearly.
@monthly 0 0 1 * * Run once a month (on the 1st).
@weekly 0 0 * * 0 Run once a week (Sunday).
@daily 0 0 * * * Run once a day (at midnight).
@midnight 0 0 * * * Same as @daily.
@hourly 0 * * * * Run every hour.

You can also use these macros in your crontab file for simplicity. No need to manually type out all those numbers!

Crontab Types: Which One Are You Editing? #

There are different types of crontabs that you may be working with:

  • User crontab: This is where your user sets their own cron jobs. You can access it by running the command:
      crontab -e
    
  • System crontab: This is a system-wide crontab located at /etc/crontab, and it’s where cron jobs that affect the whole system are configured. It requires the user field, which specifies which user the job should run as. You would edit this file with:
      sudo nano /etc/crontab
    

Example of a system crontab entry:

# m h dom mon dow user  command
0 0 * * * root /path/to/script.sh

The Role of MAILTO in Crontab #

By default, when a cron job produces any output (whether it’s a success message or an error), cron tries to send an email with that output. The MAILTO variable controls where this output goes.

Here’s how it works:

  • MAILTO=β€œ[email protected]”: Cron will send all output (standard and error) to the specified email address. Example:
      MAILTO="[email protected]"
      0 0 * * * /your/script.sh
    

    With this, you’ll receive an email every time the script produces any output.

  • MAILTO=””: This prevents cron from sending emails. If your job produces any output (even errors), cron will silently ignore it. Example:
      MAILTO=""
      0 0 * * * /your/script.sh
    

    This can be helpful if you don’t want to clutter your inbox with cron job outputs.

Best Practices for Cron Jobs #

While cron jobs are pretty straightforward, here are some best practices to keep your crontab organized and efficient:

  1. Always set MAILTO=β€β€œ if you don’t need emails. This prevents unnecessary notifications.
  2. Be explicit about paths. Cron jobs run with a minimal environment. Always provide full paths to the commands you want to run, and even the environment variables.
  3. Test your commands manually before adding them to crontab to ensure they work properly.
  4. Use cron’s macros for simplicity and readability.

Real-World Example: Controlling an LED #

I have a small SBC Server and want to automate its LED by cron job.

I added two cron jobs to the user’s list:

0 0 * * * /bin/echo none > /sys/class/leds/green_led/trigger
30 6 * * * /bin/echo heartbeat > /sys/class/leds/green_led/trigger

I get the following message:

crontab: installing new crontab

This will turn the LED off at midnight and set it to heartbeat mode at 6:30 AM. Simple and effective.

Final Thoughts #

Crontab is the heartbeat of automated task scheduling on Linux. It gives you the power to automate everything from system maintenance to custom scripts.

By understanding its format, macros, and output control options, you can easily set up reliable cron jobs that work like clockwork.

Cron is the silent worker of your system β€” and with these tips, you’ll be able to make it work even harder for you.

TL;DR for crontab command #

crontab

Schedule cron jobs to run on a time interval for the current user. More information: https://crontab.guru/.

Edit the crontab file for the current user:

crontab -e

Edit the crontab file for a specific user:

sudo crontab -e -u user

Replace the current crontab with the contents of the given file:

crontab path/to/file

View a list of existing cron jobs for current user:

crontab -l

Remove all cron jobs for the current user:

crontab -r

Sample job which runs at 10:00 every day (* means any value):

0 10 * * * command_to_execute

Sample crontab entry, which runs a command every 10 minutes:

*/10 * * * * command_to_execute

Sample crontab entry, which runs a certain script at 02:30 every Friday:

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

all tags