Linux scheduled task execution method at command and crontab service

Source: Internet
Author: User
In Linux, you can run the at command and the crontab service regularly. (1) if we only want to run a specific task with the at command, 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...

In Linux, you can run the at command and the crontab service regularly.
 
(1) at Command
 
If we only want to run a specific task once, the at monitoring program will be used at this time.
 
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 + DAT Time
 
-----------------------------------------------------------------------
Time example
-----------------------------------------------------------------------
Minuteat 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, ubuntu is started by default. 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 the service status. you can also automatically start the service when the system starts:
 
Add:
 
/Sbin/service crond start now the Cron service is already in the process, and we can use this service. The Cron service provides the following interfaces for you to use:
 
1. directly use the crontab command to edit
 
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 the cron service of a user, for example, view the cron settings of the root user: 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)
Example of the crontab file of the 6th column command to be run:
 
# 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

This article is from "mini"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.