Article Title: Two scheduled task execution methods in linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
(1) at command
If we want to run a specific task only once, we need to use the at monitoring program.
Setting the at command is very easy. It indicates the time at which the command will be run. At is similar to the print process. It puts the task in the/var/spool/at directory and runs it at the specified time. The at command is equivalent to another shell. when running the at time command, it sends commands one by one and can input any command or program. The at now + time command can be used to indicate the task.
Assume that a large database needs to be processed without a system, for example, at 03:10 AM. Then we should first establish the/home/kyle/do_job script management database and plan to process the results in the/home/kyle/do_job file. The normal method is to start the following command:
# At 2: 05 tomorrow
At>/home/kyle/do_job
At> Ctrl + D
Time Representation in AT Time
-----------------------------------------------------------------------
Time example
-----------------------------------------------------------------------
Minute at now + 5 minutes task run in 5 minutes
Hour at now + 1 hour task run in 1 Hour
Days at now + 3 days task run in 3 Days
Weeks at now + 2 weeks task run in two Weeks
Fixed at midnight task runs at midnight
Fixed at pm
Note: Check whether the atq service is enabled. Some operating systems may not be started by default. linux does not start by default, whereas ubuntu does. Run the service atd check syntax, run the service atd status Command to check the atd status, and run the service atd start command to start the atd service.
View the specific content of at execution: It is generally located under the/var/spool/at directory and opened with vi. The last part is your execution program.
(2) crontab
Cron is a linux scheduled execution tool that can run jobs without human intervention. Since Cron is a built-in service in Linux, but it does not automatically get up, you can use the following methods to start and close this service:
/Sbin/service crond start // start the service
/Sbin/service crond stop // close the service
/Sbin/service crond restart // restart the service
/Sbin/service crond reload // reload the configuration
/Sbin/service crond status // view service status
You can also enable this service automatically when the system starts:
Add:
/Sbin/service crond start
Now that the Cron Service is in the process, we can use it. The Cron Service provides the following interfaces for you to use:
1. directly use crontab for editing
The cron Service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command:
Crontab-u // set a user's cron service. Generally, the root user needs this parameter 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, to view your cron settings as root: crontab-u root-l
For another example, root wants to delete fred's cron settings: crontab-u fred-r
Basic Format:
* *** Command
Hour, day, month, and week commands
The 1st column indicates minute 1 ~ 59. Each minute is represented by * or */1.
The first column indicates the hour 1 ~ 23 (0 indicates 0 points)
The 3rd column indicates the date 1 ~ 31
The 4th column indicates the month 1 ~ 12
The Identification Number of column 5th is from day of the week to day ~ 6 (0 indicates Sunday)
6th columns of commands to run
Some examples of crontab files:
# Restart apache at every night.
30 21 ***/usr/local/etc/rc. d/lighttpd restart
#1, 10, and 22 every month
45 4, 10, 22 **/usr/local/etc/rc. d/lighttpd restart
#06:10 every morning
10 6 **** date
# Every two hours
0 */2 * date
# Every two hours from PM to am, am
0 23-7/2, 8 **** date
# Am on the 4th day of each month and from Monday to Wednesday of each week
0 11 4 * mon-wed date
# A.m. of July
0 4 1 jan * date