This article describes two ways to perform tasks on a regular basis under Linux: at command, and Crontab service.
(1) at command
If we just want to make a particular task run once, then we need to use the at monitor.
Setting the AT command is simple, indicating the time of the scheduled run, and then when it will run. At a similar print process, the task is placed in the/var/spool/at directory and runs at a specified time. The AT command is the equivalent of another shell, which, when run at the time command, sends a command to enter any command or program. The at now + Time command can be used to indicate the task.
Suppose you work with a large database to process data when someone is not using the system, like 3:10 A.M.. Then we should build the/home/kyle/do_job script Management database and plan to process the results in the/home/kyle/do_job file. The normal way to do this is to start the following command: # at 2:05 tomorrow at>/home/kyle/do_job at> ctrl+d
The time representation method-----------------------------------------------------------------------Time example in the at moment is illustrated------------------- ----------------------------------------------------Minuteat now + 5 minutes task runs in 5 minutes Hour at now + 1 Hour task runs after 1 hours At now + 3 day task run Weeks at now + 2 Weeks task to run fixed at midnight task at midnight at 10:30
Note: Be sure to check that the ATQ service is started, some operating systems may not be started by default, Linux defaults to not start, and Ubuntu defaults to boot. Check whether to start, with service ATD Check grammar, with Service ATD status Check ATD State, with Service ATD start ATD Service.
To view the specific contents of at execution: generally located under the/var/spool/at directory, open with VI, in the last part of your execution program
(2) Crontab
Cron is a regular execution tool under Linux that can run jobs without human intervention. Because Cron is a built-in Linux service, it does not automatically get up, you can start and turn off this service in the following ways:/sbin/service crond start//start service/sbin/service crond stop//shutdown service/sbin/ Service Crond restart//restart services/sbin/service crond reload//reload configuration/sbin/service crond status//view service status
You can also start this service automatically when the system is started:
At the end of the/etc/rc.d/rc.local script, add:/sbin/service crond start
Now that the Cron service is in the process, we can use this service, and the Cron service provides the following interfaces for everyone to use:
1. Edit directly with crontab command
The Cron service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command: crontab-u//Set a user's Cron service, which is required by the general root user when executing this command crontab-l// List the details of a user's Cron service crontab-r//delete a user's Cron service crontab-e//Edit a user's cron service
For example, root view your cron settings: Crontab-u root-l
Again for example, Root wants to delete Fred's cron settings: Crontab-u fred-r
Basic format: *****command Time and Moon Week command the 1th column indicates that the minute 1~59 per minute with * or */1 represents the 2nd column for the hour 1~23 (0 for 0 points) The 3rd column indicates the date 1~31 4th column represents the month 1~12 5th column identification Number week 0~6 (0 indicates Sunday) 6th Column the command to run
Some examples of crontab files: #每晚的21:30 restart Apache. * * * * * * */usr/local/etc/rc.d/lighttpd restart #每月1, 10, 22nd 4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart #每天早上6点 10 minutes 6 * * * Date #每两个小时 0 */2 * * * Date #晚上11点到早上8点之间每两个小时, 8 0 23-7/2,8 * * * Date #每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点 0 11 4 * mon-wed Date #1月份日早上4点 0 4 1 * Date