How to Use crontab Like a Pro
Table of Contents
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:
- Minute (0-59) β When during the hour the command should run (e.g., 0 means at the start of the hour).
- Hour (0-23) β The hour at which the command should run (e.g., 14 means 2 PM).
- Day of Month (1-31) β The day of the month to execute the command (e.g., 15 for the 15th).
- Month (1-12) β The month of the year when the command should execute (e.g., 5 for May).
- 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:
- Always set MAILTO=ββ if you donβt need emails. This prevents unnecessary notifications.
- 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.
- Test your commands manually before adding them to crontab to ensure they work properly.
- 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