Linux scheduling Task Cron is a timed mode, for example, we can back up the system at least when the system is used, but it is impossible for an administrator to come to work in the middle of the night, so you can use the Linux program task Cron, now to explain how to use it.
Currently popular Linux systems are installed and are randomly activated after installation.
Start cron service/sbin/service Crond start
Stop service/sbin/service Crond stop
Restart Service/sbin/service Crond restart
Reload/sbin/service Crond Reload
Start of Service Stop introduction The following is how to add a scheduled task
Using the crontab command
Crontab-u set a user's Cron service
CRONTAB-L lists the details of the current user's Cron service
Crontab-r Delete the current user's Cron service
CRONTAB-E Edit the current user's Cron service
For example, the user currently logged on is root
Running CRONTAB-E creates a scheduled task for the root user
Run crontab-e into VI Edit Schedule task content
1th-minute Representation (0-59) 2nd digit Hour (0-23)
The 3rd digit represents the day (1-31) 4th digit Month (1-12)
The 5th place represents the week (0-6) 0 means Sunday
The back www.111cn.net is the task you're going to perform.
Use spaces to separate each digit
There are several special symbols in addition to using numbers.
"*" means that all values, such as the first digit using * means every minute
"/" means every 5 minutes if the first person uses */5
"-" to denote a range of values "," to separate discrete values such as the 2nd digit is 1-6,8 1 to 6, and 8.
Specific examples are as follows
Write a message to a file at 1 O ' Day
0 1 * * * echo "HelloWorld" >>/tmp/test.txt
To execute a program or command every 5 minutes.
*/5 * * * * * * * command to execute
How to know if your scheduled task is running, 1 you can check if the command you want to execute is running at the time you specify, and 2 of the commands run poorly. You can check the cron logs/var/log/cron
The status of scheduled task execution is documented in this file
The above method adds a file with a user name in the/var/spool/cron directory after the completion of the scheduled task, and the file is your scheduled task, and the Cron service reads the files from/var/spool/cron every minute
There is also a way to add a scheduled task to edit the/etc/crontab yourself (the cron service also reads/etc/crontab files every minute)
Adding a scheduled task at the end of a file is exactly the same as the above method
There are other things in/etc/crontab, below.
mailto=root//If there is an error, or if there is data output, the data is sent to the account as a message
* * * * * root run-parts/etc/cron.hourly every hour to execute the script within/etc/cron.hourly
0 * * * * rootrun-parts/etc/cron.daily every day to execute the/etc/cron.daily inside the script
2 * * 0 root run-parts/etc/cron.weekly every week to execute a/etc/cron.weekly script.
4 1 * * Root run-parts/etc/cron.monthly every month to execute the script within/etc/cron.monthly
Note that the above run-parts parameter if you remove Run-parts followed by a script name to be executed, if you add the Run-parts parameter will be followed by the directory name
You can also restrict the use of cron
If Cron.allowcron.deny files exist in the/etc/directory
Only users listed in the Cron.allow file can use the Cron service while ignoring the Cron.deny file
If the Cron.allow file does not exist, users listed in the Cron.deny file will be barred from using the cron service.
This completes the Linux program task Cron learning.
Add some examples:
Some examples of crontab files:
* * * */usr/local/etc/rc.d/lighttpd restart
The above example shows the 21:30 restart Apache per night
4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example represents the 4:45 restart Apache for 1, 10, and 22nd per month
1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows the 1:10 reboot of Apache 6 and Sunday per week
0,30 18-23 * * * */usr/local/etc/rc.d/lighttpd restart
The example above indicates that Apache is restarted every 30 minutes from 18:00 to 23:00 every day.
0 * * 6/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example http://www.111cn.net represents 6 11:00 pm restart Apache per week
* */1 * * * */usr/local/etc/rc.d/lighttpd restart
Restart Apache every hour.
* 23-7/1 * * * */usr/local/etc/rc.d/lighttpd restart
Every hour between 11 o'clock and 7 in the morning to restart Apache.
0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart
4th per month and 11 points per week to week 3 to restart Apache.
0 4 1/USR/LOCAL/ETC/RC.D/LIGHTTPD * Restart
4 points of the month's number to restart Apache