This article describes two methods for performing tasks under Linux: At command, and the crontab service.
(1) at command
If we just want to have a specific task run once, then it's time to use the AT Monitoring program.
Setting the AT command is simple, indicating the time of the run, and then at which point it will run. At similar to the print process, the task is placed in the/var/spool/at directory and run at a specified time. The AT command is the equivalent of another shell, and when the at time command is run, it sends commands that can enter arbitrary commands or programs. The at now + Time command can indicate a task.
Suppose you are dealing with a large database, to process data when others do not use the system, such as 3:10 A.M.. Then we should set up 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 tomorrowat>/home/kyle/do_jobat> ctrl+d
The time representation method in
-----------------------------------------------------------------------Time Examples Description-----------------------------------------------------------------------Minuteat now + 5 minutes task runs after 5 minutes hour At now + 1 hour The task runs after 1 hours at the now + 3 days task after 3 day run weeks at now + 2 weeks task running fixed at midnight
task runs at midnight fixed at 10:30pm task at night 10:30
Note: Be sure to check if the ATQ service is started, some operating systems may not be the default boot, Linux default is not started, and Ubuntu default is started. Check whether it is started, check the syntax with service ATD, check the status of ATD with service ATD status, and start the ATD Service with Service ATD start.
Check the details of the at execution: generally located under the/var/spool/at directory, with VI open, in the last part is your execution program
(2) Crontab
Cron is a timed execution tool under Linux that can run a job without human intervention. Since Cron is a built-in service for Linux, it does not automatically get up, and you can start and shut down this service in the following ways:
/sbin/service crond start//Start service/sbin/service crond stop//shutdown service/sbin/service crond restart //Restart service/sbin/service Crond reload//Reload Configuration/sbin/service crond status//view service status
You can also start the service automatically when the system starts:
At the end of the/etc/rc.d/rc.local script, add:
/sbin/service Crond Start
Now cron This service is already in the process, we can use this service, Cron service provides the following kinds of 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 cron service for a user, typically the root user needs this parameter when executing this command crontab-l//list details of a user cron service Crontab-r//delete a user's Cron service CRONTAB-E/ /Edit a user's cron service
For example, root to 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-of-day week the 1th column represents minutes 1~59 per minute with * or */1 to indicate that the 2nd column represents the hour 1~23 (0 means 0 points) The 3rd column represents the date 1~31 4th column represents the month 1~12 5th column identification Number Week 0~6 (0 = Sunday) 6th column to run the command
Some examples of crontab files:
#每晚的21:30 Restart Apache. * * * */usr/local/etc/rc.d/lighttpd restart# 1, 10, 22nd per month 4 1,10,22 * */usr/local/etc/rc.d/lighttpd restart# daily 6:10 A.M. 6 * * * date# every two hours 0 */2 * * * date# 11 o'clock to 8 a.m. every two hours in the Morning 8 o ' 23-7/2,8 * * * date# 0 per month and Monday to Sunday 4th three * Mon-we D date#1 month Morning 4 0 4 1 Jan * Date
Linux timed Task execution