Cron is a Linux scheduled execution tool that can run jobs without human intervention. The cron daemon reads the crontab file and executes the task at the specified time according to the configuration. The contab command is used to add, delete, and display a cron task table. (Ubuntu environment) you can use the service command to start and stop the cron service: servicecronstatus # view the cron service status
Cron is a Linux scheduled execution tool that can run jobs without human intervention. The cron daemon reads the crontab file and executes the task at the specified time according to the configuration.
The contab command is used to add, delete, and display a cron task table.
(Ubuntu environment) you can use the service command to start and stop the cron service:
Service cron status # view the cron service status
Service cron start # start the cron service
Service cron stop # stop the cron service
Service cron restart # restart the cron service
Service cron reload # reload the cron service configuration
Crontab command format
Crontab [-u user] file
Crontab [-u user] [-l |-r |-e] [-I] [-s]
Command parameters
-U
Specified user
-L
Displays the contents of the current crontab file.
-R
Delete the contents of the current crontab file.
-E
Edit the content of the current crontab file. if the file does not exist, create a new file.
-I
A prompt is displayed when you delete the crontab file.
Crontab file
Format:Minute hour day_of_month month day_of_week command
Field description
Field |
Description |
Minute |
Minute, value range: (0-59) |
Hour |
Hour, value range: (0-23) |
Day_of_month |
Date, value range: (0-31) |
Month |
Month. the value range is (1-12). It can also be expressed by jan, feb, mar, apr .... |
Day_of_week |
Week, value range: (0-6), where Sunday is represented by 0 or 7, or sun, mon, tue, wed, thu, fri, sat. |
Command |
Command to be executed |
Special symbol meaning
Special symbols |
Description |
* |
Wildcard all possible values |
, |
Specifies the value list, such as, 9 |
- |
Specify the integer range, for example, 1-5. |
/ |
Interval |
Cron expression example
Example |
Description |
*/15 **** |
Execute tasks every 15 minutes |
0 */2 *** |
Execute the task at the hour every two hours. |
0 3 **** |
Execute the task at every day. |
0 0 5, 15, 25 ** |
Execute the task on the 5th, 15th, and 25th of each month. |
15 12 ** 1 |
Execute the task at every Monday. |
12-20 *** |
Execute the task between and every day at the hour or half. |
Instance
A) add a cron task
huey@huey-K42JE:~/huey/linux/cmdline$ echo "*/3 * * * * date >> ~/huey/linux/cmdline/cron.out" > mycronhuey@huey-K42JE:~/huey/linux/cmdline$ crontab mycron
B) delete a cron task
huey@huey-K42JE:~/huey/linux/cmdline$ crontab -r
C) display cron tasks
huey@huey-K42JE:~/huey/linux/cmdline$ crontab -l*/3 * * * * date >> ~/huey/linux/cmdline/cron.out