Common Linux commands: crontab and linuxcrontab
Crontab is a command used to set, delete, or display scheduled tasks executed by the cron daemon. Each user can have their own scheduled tasks. By default, the scheduled task file is named by the user name and placed in the/var/spool/cron Directory, which is not accessible to common users.
You can use the cron. allow and cron. deny files to manage the permissions for users to use the cron service. If cron. allow exists. The cron service is allowed only when the user is listed. deny exists, and users listed in it are prohibited from using the cron service. If neither of them exists, only super users can use the cron service. In CentOS, the two files are placed in/etc. By default, only the cron. deny file exists and is empty. This means that in CentOS, by default, all users have cron permission.
1. Command Format
Crontab [-u user] file
Crontab [-u user] [-l |-r |-e] [-I] [-s]
Crontab-n [hostname]
Crontab-c
2. command functions
Maintenance of scheduled task files for individual users
3. Command Options
-U user
Edit the cron of a user. Only root can use this parameter to set the cron service for other users. You can also specify a crontab file.
-L
Lists the crontab of the current user.
-R
Delete the crontab of the current user.
-E
Edit the current user's crontab. vi is used by default, or the EDITOR specified by VISUAL or EDITOR environment variables.
-I
It is used with-r to ask whether to delete the crontab directly.
4. Instance
Instance 1: List scheduled tasks of the current user
[22:30:17][dodmaster@mha3 ~]$ crontab -l0 1 * * 1 $HOME/.DailyShell/hislogclear.sh 0 * * * * $HOME/.DailyShell/mvcdr2bak.sh $HOME/data/message/voice20 * * * * $HOME/.DailyShell/mvcdr2bak.sh $HOME/data/message/data/40 * * * * $HOME/.DailyShell/mvcdr2bak.sh $HOME/data/message/sms/
Example 2: the root user edits the scheduled task of user martin.
[22:30:17][root@mha3 ~]# crontab -u martin -e
Instance 3: specify a scheduled task file for user martin
[22:30:17][root@mha3 ~]# crontab -u martin martincron
5. cron File Syntax
First, open the/etc/crontab file and we will see the following content:
[20:40:30][dodmaster@mha3 etc]$ cat /etc/crontab SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=rootHOME=/# For details see man 4 crontabs# Example of job definition:# .---------------- minute (0 - 59)# | .------------- hour (0 - 23)# | | .---------- day of month (1 - 31)# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat# | | | | |# * * * * * user-name command to be executed
This file is a scheduled task for the system to run. The first four rows are used to set the environment variables for running the cron service: SHELL, PATH, and HOME specify the SHELL environment variables, PATH environment variables, and HOME environment variables when the cron service is running. MAILTO indicates that the output of the task running cron is sent to the specified user by email. If the value of this variable is null, no email is sent. The remaining lines describe the specific writing format of the system scheduled task.
Minute |
Minute, value range: 0-59 |
Hour |
Hour, value range: 0-23 |
Day of month |
Day, value range: 1-31 |
Month |
Month, with a value ranging from 1 to 12, or jan, feb, mar, apr... |
Day of week |
Week. The value range is 0-6, 0, or 7, indicating Sunday, or sun, mon, tue, wed, thu, fri, sat. |
User-name |
The user who executes the scheduled task |
Command |
A specific command can be a simple command, a script, or a directory. If it is a folder, all scripts in the folder are executed. run-parts must be added before the file directory. |
1) asterisks (*) indicate all values in the value range. For example, * indicates that the task is executed every hour at the hour location.
2) hyphens (-) indicate a range. For example, 8-12 indicates 8, 9, 10, 11, and 12.
3) comma (,) indicates the specified value. For example, 3,5-7,9 indicates 3,5, 6,7, and 9.
4) forward slash (/) indicates the step value. For example, if the minute is */5, the execution is performed every five minutes.
All user-defined crontab scheduled tasks except the root user are stored in the/var/spool/cron directory.Crontab-eCommand editing. The format is the same as that of/etc/crontab. You do not need to specify the user-name.
The cron service checks and executes all files in/etc/crontab,/etc/cron. d/, And/var/spool/cron/every minute.
6. Instance
Instance 1: perform a system raid-check at one o'clock every weekend.
0 1 * * Sun root /usr/sbin/raid-check
Example 2: at am every day, the root user executes all scripts in the/etc/cron. daily directory. The run-parts parameter indicates that all scripts in the subsequent directories are executed.
02 4 * * * root run-parts /etc/cron.daily
Example 3: The dodmaster user executes the hislogclear. sh script at, and every four hours every day.
[21:57:56][dodmaster@mha3 ~]$ crontab -l44 8-20/4 * * * $HOME/.DailyShell/hislogclear.sh
Instance 4: Run Once every five minutes
*/5 * * * * echo "every five minute to do it" >> time.txt
Instance 5: Run at eight o'clock P.M. every business day
0 20 * * 1-5 echo "every workday 20:00 to do it" >> time.txt
Instance 6: 30 seconds per minute. Because the cron service is awakened once every minute, the sleep command must be used to execute tasks precise to seconds.
* * * * * sleep 30; echo "every minute 30 second to do it" >> time.txt
Instance 7: execution every 20 seconds, that is, execution every 0 s, 20 s, and 40 s per minute (or another step size is 20 s, for example: 5 s, 25 s, 45 s)
* * * * * echo "every 20 second to do it" >> time.txt* * * * * sleep 20; echo "every 20 second to do it" >> time.txt* * * * * sleep 40; echo "every 20 second to do it" >> time.txt
References:
Http://www.cnblogs.com/dingyingsi/archive/2013/04/16/3023623.html
Http://blog.csdn.net/xiyuan1999/article/details/8160998
Http://blog.chinaunix.net/uid-7552018-id-182133.html