Timed Task Crontab Detailed

Source: Internet
Author: User

Cron is a daemon that can be used to schedule execution of repetitive tasks based on a combination of time, date, month, and week.

Cron assumes that the system is running continuously. If the system is not running when a task is scheduled, the task is not executed.

To use the Cron service, you must install the Vixie-cron RPM package and must be running the Crond service. To determine if the package is installed, use the Rpm-q vixie-cron command. To determine whether the service is running, use the/sbin/service crond status command. 37.1.1. Configuring cron Tasks

The primary configuration file for Cron is/etc/crontab, which includes the following lines:

Shell=/bin/bash
                  path=/sbin:/bin:/usr/sbin:/usr/bin
                  mailto=root
                  home=/
                  # run-parts
                  01 * * * * * * Root run-parts/etc/cron.hourly
                  4 * * * * Root run-parts/etc/cron.daily
                  4 * * 0 root Run-parts/etc/cron.week LY
                  4 1 * * Root run-parts/etc/cron.monthly

The first four lines are variables used to configure the Cron task's running 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 master directory to use when executing commands or scripts.

Each row in the/etc/crontab file represents a task, and its format is:

Minute         hour         Day         month         DayOfWeek         command


* * * * command
Time-sharing and Lunar Week command

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

In each of these fields, you can also use the following special characters:

Asterisk (*): represents all possible values, such as the month field if it is an asterisk, indicates that the command operation is performed every month after the constraints of other fields are met.

Comma (,): You can specify a list range with a comma-separated value, for example, "1,2,5,7,8,9"

Medium Bar (-): You can use the middle bar between integers to represent an integer range, for example, "2-6" means "2,3,4,5,6"

Forward slash (/): You can use a forward slash to specify the interval frequency of the time, for example, "0-23/2" means to perform every two hours. The forward slash can also be used with the asterisk, such as */10, which, if used in the minute field, means that it is executed every 10 minutes.

Second, Crond service

Install Crontab:

Yum Install Crontabs

Service Operation Description:

/sbin/service crond Start//boot service

/sbin/service Crond Stop//Off service

/sbin/service crond Restart//Restart service

/sbin/service Crond Reload//Reload Configuration

To view the Crontab service status:

Service Crond Status

To manually start the Crontab service:

Service Crond Start

To see if the Crontab service is set to boot, execute the command:

Ntsysv

Join the boot auto start:

Chkconfig–level Crond on

Base command:

-e indicates editing the current crontab

-L indicates that the list displays the current crontab task

-R indicates deletion of the current user's crontab

-I gives prompt information when deleting crontab


5. Working with instances

Example 1: Execute command every 1 minutes

Command:

* * * * command

Example 2:3rd and 15 minutes per hour execution

Command:

3,15 * * * command

Example 3:3rd and 15 minutes from 8 o'clock in the morning to 11.

Command:

3,15 8-11 * * * command

Instance 4:3rd and 15 minutes from 8 o'clock in the morning to 11 points every two days

Command:

3,15 8-11 */2 * command

Instance 5:3rd and 15 minutes per Monday from 8 o'clock in the morning to 11.

Command:

3,15 8-11 * * 1 command

Example 6:21:30 restart SMB per night

Command:

* * * */ETC/INIT.D/SMB restart

Instance 7:4:45 Restart SMB for 1, 10, 22nd per month

Command:

4 1,10,22 * */ETC/INIT.D/SMB restart

Example 8:1:10 restart SMB per Saturday and Sunday

Command:

1 * * 6,0/ETC/INIT.D/SMB restart

Example 9: Restart SMB every 30 minutes from 18:00 to 23:00 every day

Command:

0,30 18-23 * * * */ETC/INIT.D/SMB restart

Example 10: Every Saturday night 11:00 pm reboot SMB

Command:

0 * * 6/ETC/INIT.D/SMB restart

Example 11: Restart SMB per hour

Command:

* */1 * * * */ETC/INIT.D/SMB restart

Example 12: Restart SMB every hour between 11 o'clock and 7 in the morning.

Command:

* 23-7/1 * * * */ETC/INIT.D/SMB restart

Example 13:4th for each month and restart SMB for 11 points every Monday to Wednesday

Command:

0 4 * MON-WED/ETC/INIT.D/SMB restart

Example 14: Restart SMB at 4 point January 1

Command:

0 4 1/ETC/INIT.D/SMB * Restart

Example 15: Executing scripts within the/etc/cron.hourly directory every Hour

Command:

* * * * Root run-parts/etc/cron.hourly

Description

Run-parts This parameter, if you remove this parameter, then you can write a script name to run, not a directory name

Iv. Precautions for use

1. Pay attention to environment variable problem

Sometimes we create a crontab, but this task is not automated, but it is not a problem to perform this task manually, which is typically caused by not configuring the environment variable in the crontab file.

When you define multiple scheduling tasks in the crontab file, one problem that needs special attention is the setting of environment variables, because when we perform a task manually, it is in the current shell environment, and the program can certainly find the environment variable, and the system does not load any environment variables when it performs task scheduling automatically. As a result, you need to specify all the environment variables required for the task to run in the crontab file, so that there is no problem when the system performs task scheduling.

Do not assume that Cron knows the special circumstances that are required, it does not actually know. So you have to make sure that you provide all the necessary path and environment variables in the Shelll script, in addition to some of the global variables that are automatically set. So notice the following 3 points:

1 Write the global path when the file path is involved in the script;

2 when the script executes to use Java or other environment variables, the source command is used to introduce environment variables, such as:

Cat start_cbp.sh

#!/bin/sh

Source/etc/profile

Export run_conf=/home/d139/conf/platform/cbp/cbp_jboss.conf

/usr/local/jboss-4.0.5/bin/run.sh-c MeV &

3 When the script is executed manually OK, but crontab is not executed. At this time must be bold suspicion of environmental variables, and can try to directly introduce environmental variables in the crontab to solve the problem. Such as:

0 * * * *. /etc/profile;/bin/sh/var/www/java/audit_no_count/bin/restart_audit.sh

2. Pay attention to cleaning up the mail log of system users

Each task scheduled execution completed, the system will send the task output information through e-mail to the current system users, so that the accumulation of log information will be very large, may affect the normal operation of the system, so it is important to redirect each task.

For example, you can set the following form in the crontab file to ignore the log output:

0 */3 * * */usr/local/apache2/apachectl restart >/dev/null 2>&1

The "/dev/null 2>&1" means that the standard output is redirected to the/dev/null first, then the standard error is redirected to the standard output, and the standard error is redirected to the/dev/null because the standard output has been redirected to the/dev/null. This will solve the problem of log output.

3. System-level task scheduling and user-level task scheduling

System-level task scheduling mainly completes some maintenance operations of the system, user-level task scheduling mainly completes a number of user-defined tasks, you can put user-level task scheduling to the system-level task scheduling to complete (not recommended), but the reverse is not, the root user task scheduling operations can be through the "crontab– Uroot–e "To set up, you can also write the dispatch task directly to the/etc/crontab file, it should be noted that if you want to define a task that periodically restarts the system, you must put the task in the/etc/crontab file. Even the task of creating a timed reboot system under the root user is not valid.

4. Other considerations

The newly created Cron job will not be executed immediately and will take at least 2 minutes to execute. If you restart Cron, it executes immediately.

When Crontab suddenly fails, you can try to/etc/init.d/crond restart solve the problem. Or check the log to see if a job has executed/error tail-f/var/log/cron.

Don't run crontab-r. It deletes the user's crontab file from the crontab directory (/var/spool/cron). All crontab that deleted the user are gone.

In Crontab, the% is of special meaning, indicating the meaning of line wrapping. If you want to use it must be escaped \%, such as the frequent use of the date ' +%y%m%d ' in the crontab will not be executed, should be replaced by the date ' +\%y\%m\%d '.


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.