Cron is a timed execution tool under Linux that can run a job without human intervention. The daemon cron reads the crontab file and executes the task at a specified time, depending on the configuration.
The Contab command is used to add, delete, and display cron task tables.
(Ubuntu Environment) can start and stop the Cron service via service command:
Service Cron Status # View Cron service state
Service Cron Start # start cron service
Service cron Stop # Stop cron service
Service Cron Restart # restart Cron Service
Service Cron Reload # reload Cron service configuration
crontab Command Command format
crontab [-u user] File
crontab [-u user] [-l |-r |-e] [-I.] [-S]
Command parameters
-U
Specify user
-L
Displays the contents of the current Crontab file.
-R
Deletes the contents of the current Crontab file.
-E
Edits the current crontab file content and creates a new file if the file does not exist.
-I.
Be prompted when you delete the contents of the crontab file.
crontab file
format:minute hour day_of_month month Day_of_week command
Field meaning
Field |
Meaning |
Minute |
Minutes, the value range is (0-59) |
Hour |
Hours, the value range is (0-23) |
Day_of_month |
Date, value range is (0-31) |
Month |
month, the value range is (1-12), you can also use jan,feb,mar,apr ... Said |
Day_of_week |
Week, the value range is (0-6), of which Sunday is 0 or 7, can also be expressed in Sun,mon,tue,wed,thu,fri,sat |
Command |
The command to execute |
Special Symbolic meaning
Special symbols |
Description |
* |
Pass all possible values |
, |
Specify a list of values, such as 1,3,5,7,9 |
- |
Specifies an integer range, such as 1-5 |
/ |
Specify interval frequency |
Cron Expression Example
Example |
Description |
*/15 * * * * |
Perform tasks every 15 minutes |
0 */2 * * * |
Perform the task every 2 hours on the hour |
0 3 * * * * |
Perform tasks three o'clock in the morning every day. |
0 0 5,15,25 * * |
Every month, number 5th, 15th, Morning 25th. |
15 12 * 1 |
Every Monday, 12:15 noon, the mission is performed. |
0,30 12-20 * * * |
Every day from 12:00 to 20:00, at the hour or half of the time to perform tasks |
Instance
a) adding cron tasks
echo "*/3 * * * * date >> ~/huey/linux/cmdline/cron.out" > Mycron crontab Mycron
b) Delete Cron task
Crontab-r
c) Display cron task
crontab-l*/3 * * * * Date >> ~/huey/linux/cmdline/cron.out
Linux Commands-crontab: Task Scheduler