Linux-crontab Scheduled Tasks

Source: Internet
Author: User

The crontab command is commonly used in UNIX and Linux operating systems to set instructions that are executed periodically. The command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution. Typically, crontab stored instructions are activated by the daemon. Crond often runs in the background, checking to see if a scheduled job needs to be performed every minute. This type of work is generally called cron jobs.

First, installation

[[Email protected] ~] # yum-y Install Vixie-cron [[Email protected] ~] # yum-y Install Crontabs

Description
Vixie-cron package is the main program of Cron;
The Crontabs package is a program used to install, uninstall, or list tables used to drive the cron daemon.

There are two ways to add a dispatch task:

       1), in the command line input: crontab-E and then add the corresponding task, Wq save the disk to exit.         2), directly edit the/etc/crontab file, that is, vi/etc/crontab, add the corresponding task.

View scheduling Tasks

        Crontab-l// list all current scheduled        tasks -l-u JP   //list all scheduling tasks for user JP

Second, the configuration

Cron is a built-in service for Linux, but it does not automatically get up, and you can start and shut down this service in the following ways:

Service Crond start     // Start Services service Crond stop      // Turn off services crond restart   / / Restart Service Crond Reload    // reload Config service    crondstatus//view Crontab service state

Add power-on to the CentOS system to boot automatically:

Chkconfig--level 345 Crond on

The main configuration file for cron is /etc/crontab, which includes the following lines:

shell=/bin/bashpath=/sbin:/bin:/usr/sbin:/usr/binmailto=roothome#  Run-parts* * * * root run-parts/etc/cron.hourly4 * * * Root run-parts/etc/cron.daily22 4 * * 0 root run-parts/etc/cron.weekly4 1 * * Root run-parts/etc/cron.monthly

The first four rows are variables that you can use to configure the Cron task runtime environment.
The value of the shell variable tells the system which shell environment to use (in this case, the bash shell);
The path variable defines the paths used to execute the command.
The output of the cron task is mailed to the user name defined by the MAILTO variable.
If the MAILTO variable is defined as a blank string (mailto= ""), the e-mail message is not sent.
The home variable can be used to set the home directory to use when executing a command or script.

To restrict the use of cron:

/etc/cron.allow
and /etc/cron.deny files are used to limit the use of cron.
The two formats used by the control file are one user per line.
None of the two files allow spaces.
If the control file is modified, the cron daemon (crond) does not have to be restarted.
Using control files is read every time a user adds or deletes a cron task.

Root can always use cron, regardless of the rules used in the control file.

If the Cron.allow file exists, only the users listed in it are allowed to use cron, and the Cron.deny file is ignored.
If the Cron.allow file does not exist, all users listed in Cron.deny are prohibited from using cron.

Iii. crontab Order

function : Set the timer.

Syntax :

Crontab[-u < user name >][profile] or crontab [u < user name >][-ELR]

explanation : Cron is a resident service that provides the function of a timer that allows a user to execute a preset instruction or program at a specific time. The function of the timer can be used as long as the user edits the timer's configuration file. Its configuration file format is as follows: Minute Hour day Month DayOFWeek Command

Parameters:
- e Edit the user's timer settings.
- L lists the user's timer settings.
- R removes the user's timer settings.
- u< user name > Specifies the name of the user to set the timer.

Format:

* * * * * *    command time Moon week the 1th column represents minutes 1~59 per minute with * or */1 means the 2nd column represents the hour 1~(0 means 0 points) The 3rd column represents the date 1~31
   
     the 4th column represents the month 1~the 
    5th column identification number week 0~6
    (0 for Sunday) 6th column to run the command
   

Meaning of several special symbols:

"*" represents a number in the range of values,
"/" stands for "every",
"-" represents a number to a number,
"," separate a few discrete numbers

Example:

* * * */usr/local/etc/rc.d/lighttpd Restart the above example shows that 21:30 restarts Apache per night. 4 1,10,22 * */usr/local/etc/rc.d/lighttpd Restart the above example represents 1 per month,10, 22nd of 4:45 restart Apache. 1 * * 6,0/usr/local/etc/rc.d/lighttpd Restart the above example shows that 1:10 restarts Apache every Saturday and Sunday. 0,30 18-23 * * */usr/local/etc/rc.d/lighttpd Restart the above example shows that Apache restarts every 30 minutes from 18:00 to 23:00 every day. 0 * * 6/usr/local/etc/rc.d/lighttpd Restart the above example shows every Saturday:00pm Restart Apache. * */1 * * */usr/local/etc/rc.d/lighttpd Restart restart Apache every hour* 23-7/1 * * */usr/local/etc/rc.d/lighttpd Restart from 11 o'clock to 7 in the morning, restart Apache every hour .0 4 * mon-wed/usr/local/etc/rc.d/lighttpd Restart 4th per month with 11-point restart Apache from Monday to Wednesday0 4 1 Jan */usr/local/etc/rc.d/lighttpd Restart January 1 4-point restart Apache*/30 * * * */usr/sbin/ntpdate 210.72.145.44synchronize time every half hour

Type CRONTAB-E Edit crontab service file

For example, the contents of the file are:

     */2 * * * * */bin/sh/home/admin/jiaoben/buy/deletefile.sh      Save the file and     exit */2 * * * */bin/sh/home/admin/jiaobe n/buy/deletefile.sh    */2 * * * * This field allows you to set when the script      is executed /bin/sh/home/admin/jiaoben/buy/ deletefile.sh This field can set the script you want to execute, note here that bin/sh refers to the path that the  script holds after the command that runs the script

To see if the Crontab service under this user was created successfully, use the CRONTAB-L command

See if the service is already running with Ps-ax | grep cron

crontab file [-u user]- replaces the current crontab with the specified files. Crontab-[-u user]- replaces the current Crontab.crontab -1[user]-with standard inputto list the user's current Crontab.crontab -e[user]- Edit user Current Crontab.crontab-d[user]- Delete user current Crontab.crontab-c dir- specify Crontab directory. Format of the crontab file: M H d M d cmd.

Linux-crontab Scheduled Tasks

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.