If we think about "handling tasks at regular intervals" in Linux and Unix, the first thing that comes to mind is crontab
. It’s so reassuring that the system can automatically perform repetitive tasks for us! However, it can also be a headache if you don’t understand it well, making it a presence that embodies both an angel and a devil. Based on what I've realized through wrestling with crontab
multiple times, I will share what crontab
is, how to use it, and some practical tips.
What exactly is crontab?
crontab
, simply put, is a tool that allows you to "schedule tasks to be executed automatically and periodically" in Linux and Unix systems. With this setting, I can easily handle tedious tasks that repeat daily, weekly, or monthly. Once set up, it mindlessly processes tasks for me, making the system feel like my personal assistant.
The magic behind all this is the cron daemon. The cron daemon checks the contents recorded in the crontab
file and executes commands at the specified times, which is why I enjoy using crontab
.
Let’s learn about crontab commands
crontab
has several useful commands. Knowing these well can save you time and allow you to easily check and modify your settings.
- crontab -e: This command opens the
crontab
file to add or modify tasks. - crontab -l: Displays the contents of the current user’s
crontab
file. - crontab -r: Deletes the current user’s
crontab
file. (Handle with care)
What does the crontab file format look like?
A crontab
file consists of one line for each task, with each line made up of five time fields and one command. At first glance, it might seem confusing, but it’s actually quite simple when you look at it one by one.
* * * * * /path/to/command
- - - - -
| | | | +----- Day of the week (0 - 7) (Sunday is 0 or 7)
| | | +------- Month (1 - 12)
| | +--------- Day of the month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)
Each field contains numbers or *
, where *
means any value. For example, * * * * *
means “execute every minute, every hour, every day, every month, every day of the week.” In other words, it runs every minute. With proper application, you can create a wide variety of scheduling.
Running a task every 5 minutes: A crontab example
For instance, let’s say we have the command */5 * * * * /path/to/myscript.sh
. This setting means to execute the script myscript.sh
every 5 minutes.
*/5
: Every 5 minutes*
: Any hour*
: Any day*
: Any month*
: Any day of the week
This means the script will run every 5 minutes throughout a 24-hour day. It’s so convenient to have it running periodically without worrying about it!

Various usage examples
You can set crontab
according to various situations. Let’s examine a few examples of how it can be used.
- Execute a script every hour
0 * * * * /path/to/script.sh
Executes the script at the start of every hour, e.g., 12:00, 13:00, 14:00... - Run a backup task at 3 AM daily
0 3 * * * /path/to/backup.sh
Running a backup at 3 AM daily might reduce late-night work. - Run on the 1st and 15th of every month at 2 AM
0 2 1,15 * * /path/to/script.sh
It’s also good to repeat important tasks at the beginning and middle of each month. - Run only on weekends
30 8 * * 6,7 /path/to/weekend_script.sh
This sets the script to execute at 8:30 AM every Saturday and Sunday.
Things to keep in mind when setting up crontab
In Linux, crontab
tasks are retained even after the system reboots. However, one thing to note is that if a scheduled task is set for a time when the system is turned off, it will not execute. For example, if a task is scheduled to run every day at 3 PM but the system is off and turns on at 4 PM, the 3 PM task for that day will simply be skipped.
To solve this problem, you can use a friend called anacron. Anacron is a tool that executes "missed tasks" even if the system is turned off. It complements the limitations of crontab
, making it useful for tasks that need to run regularly regardless of whether the system is on or off.
Utilizing crontab
properly can be a great help to me. By automating important tasks through time scheduling, I can save time and minimize mistakes. Of course, it may seem challenging at first due to the format, but once you get used to it, an irresistible convenience awaits. How about stepping into the world of crontab
after reading this?
Add a New Comment