Lesson 43: Cron Command
In this lesson, you'll learn how to use the cron command and crontab to schedule and run Linux tasks automatically at regular intervals.
Let's discuss the popular crontab command to schedule and run Linux tasks in the background automatically at regular intervals.
The users can automate Linux system backup, schedule updates, and synchronization of files, and many more using the Cron daemon, which is used to run scheduled tasks from the command line.
cron wakes up every minute and checks scheduled tasks in Crontab (CRON TABle) - a table where we can schedule such kinds of repeated tasks.
Tips: Each user can have their own crontab to create, modify, and delete tasks. By default, cron is enabled for users. However, we can restrict users by adding an entry in the /etc/cron.deny file.
Crontab Syntax
The Crontab file consists of commands per line and has six fields, separated either by space or tab. The first five fields represent the time to run tasks, and the last field is for the command.
# crontab -e
* * * * * /path/to/command
β β β β β
β β β β βββ Day of Week (0-6 or Sun-Sat)
β β β βββββ Month (1-12 or Jan-Dec)
β β βββββββ Day of Month (1-31)
β βββββββββ Hour (0-23)
βββββββββββ Minute (0-59)
Field descriptions:
- Minute - holds values between
0-59 - Hour - holds values between
0-23 - Day of Month - holds values between
1-31 - Month of the Year - holds values between
1-12orJan-Dec(you can use the first three letters of each month's name, e.g.JanorJun) - Day of Week - holds values between
0-6orSun-Sat(you can also use the first three letters of each day's name, e.g.SunorWed) - Command - the
/path/to/commandor script you want to schedule
crontab Command Options
| Option | Description |
|---|---|
-l |
List current user's crontab entries |
-e |
Edit the current user's crontab |
-r |
Remove the current user's crontab |
-i |
Prompt for confirmation before removing crontab |
-u USER |
Specify the user whose crontab to manage (root only) |
1. List Crontab Entries
List or manage the task with the crontab command with the -l option for the current user.
# crontab -l
00 10 * * * /bin/ls >/ls.txt