Go to: Linux Crontab scheduled task commands

Source: Internet
Author: User
Tags crontab syntax

1. crontabIntroduction

 

The crontab command schedules the execution of some commands at certain intervals.

 

1.1/etc/crontab file

There is a crontab file in the/etc directory, which stores some scheduling tasks that are running systematically.Program. Each user can create their own scheduling crontab.

For example:

[Root @ Dave ~] # Cat/etc/crontab

Shell =/bin/bash

Path =/sbin:/bin:/usr/sbin:/usr/bin

Mailto = root

Home =/

# Run-Parts

01 *** root run-parts/etc/cron. Hourly

02 4 *** root run-parts/etc/cron. daily

22 4 ** 0 root run-parts/etc/cron. Weekly

42 4 1 ** root run-parts/etc/cron. Monthly

 

1.2/etc/cron. deny and/etc/cron. allow files

 

/Etc/cron. deny indicates that the crontab command cannot be used.

/Etc/cron. allow indicates the user who can use crontab.

 

If two files exist at the same time,/etc/cron. allow takes priority.

If neither of the two files exists, only the Super User can schedule a job.

 

 

Each user generates his/her own crontab file. These files are in the/var/spool/cron directory:

 

For example:

[Root @ Dave ~] # Cd/var/spool/cron

[Root @ Dave cron] # ls

Oracle root

 

We can directly view this file, and the content is consistent with the crontab-l displayed by the corresponding user.

[Root @ Dave cron] # Cat Oracle

00 6 ***/u02/scripts/del_st_archive.sh>/u02/scripts/del_st_arch.log 2> & 1

[Root @ Dave cron] # Cat Root

0 12 ***/root/bin/sync-clock.sh

[Root @ Dave cron] #

 

 

Ii. crontab instructions

 

2.1 crontab syntax

Usage: crontab [-u user] File

Crontab [-u user] [-E |-L |-R]

(Default operation is replace, per 1003.2)

-E (edit user's crontab)

-L (list user's crontab)

-R (delete user's crontab)

-I (Prompt before deleting user's crontab)

-S (SELinux context)

 

File is the name of the command file. If this file is specified in the command line, run the crontab command to copy the file to the crontabs directory. If this file is not specified in the command line, the crontab command will accept the commands typed on the standard input (keyboard) and store them in the crontab directory.

 

 

Help:

[Root @ Dave ~] # Man crontab

Crontab (1) crontab (1)

 

Name

Crontab-maintain crontab files for individual users (ISC cron v4.1)

 

Synopsis

Crontab [-u user] File

Crontab [-u user] [-L |-r |-E] [-I] [-S]

 

Description

crontab is the program used to install, deinstall or list the tables used to drive the cron (8) daemon in ISC cron. each user can have their own crontab, and though these are files in/var/spool/, they are not intended to be edited directly. for SELinux in MLS mode can be even more crontabs-for each range. for more see SELinux (8 ).

If the cron. allow file exists, then you must be listed therein order to be allowed to use this command. if the cron. allow file does not exist but the cron. deny file does exist, then you must not be listed in the cron. deny file in order to use this command. if neither of these files exists, only the super user will be allowed to use this command.

 

Options

-U it specifies the name of the user whose crontab is to be tweaked. if this option is not given, crontab examines "your" crontab, I. E ., the crontab of the person executing the command. note that SU (8) can confuse crontab and that if you are running inside of SU (8) You shoshould always use the-u option for safety too many s sake. the first form of this command is used to install a new crontab from some named file or standard input if the pseudo-FILENAME "-" is given.

-L the current crontab will be displayed on standard output.

-R the current crontab will be removed.

-E this option is used to edit the current crontab using the editor specified by the visual or editor environment variables. After you exit from the EDI-tor, the modified crontab will be installed automatically.

-I this option modifies the-r option to prompt the user for a temporary snapshot y/y temporary snapshot response before actually removing the crontab.

-S it will append the current SELinux security context string as an mls_level setting to the crontab file before editing/replacement occurs-see the documentation of mls_level in crontab (5 ).

 

See also

Crontab (5), cron (8)

Files

/Etc/cron. allow

/Etc/cron. deny

Standards

The crontab command conforms to IEEE std1003.2-1992 (commandid POSIX packet forwarding rule). This new command syntax differs from previous versions of vixie Cron, as well as from the classic

Svr3 syntax.

Diagnostics

A fairly informative usage message appears if you run it with a bad command line.

Author

Paul vixie <vixie@isc.org>

4th Berkeley distribution 16 Januar 2007 crontab (1)

 

 

 

 

 

2.2 crontab format description

We can use crontab-e to add the command to be executed. The command execution result, whether it is standard output or error output, will be sent to the user in the form of mail.

The added command must be in the following format:

* ***/Command path

The first five fields can take an integer to specify when to start working. The sixth field is a string, that is, a command field, which includes the crontab command for scheduling and execution. The fields are separated by spaces and tabs.

 

The first five fields are represented respectively:

Minute: 0-59

Hours: 1-23

Date: 1-31

Month: 1-12

Week: 0-6 (0 indicates Sunday)

 

You can also use some special symbols:

*: Indicates any time

,: Indicates Segmentation

-: Indicates a segment. For example, 1-5 in the second end indicates.

/N: indicates that each n is executed once. For example, in the second segment, */1 indicates that the command is executed every one hour. You can also enter 1-23/1.

 

 

Some examples:

00, 8, 12, 16 */data/APP/scripts/monitor/DF. Sh

30 2 ***/data/APP/scripts/hotbackup/hot_database_backup.sh

10 8, 12, 16 */data/APP/scripts/monitor/check_ind_unusable.sh

10 8, 12, 16 */data/APP/scripts/monitor/check_maxfilesize.sh

10 8, 12, 16 */data/APP/scripts/monitor/check_objectsize.sh

 

43 21 *** run

15 05 *** run

0 17 *** execute

0 17 ** 1 run at every Monday

17 **, 3 run at and on every Sunday, Tuesday, and Wednesday.

0-10 17 1 ** every 1 minute from to on the first day of the month

0 0*1 on the 1st and 15th of the month

42 4 1 ** executed at on the first day of the month

0 21 ** 1-6 from Monday to Saturday

0, 10, 20, 30, 40, 50 *** run every 10 minutes

*/10 * execute every 10 minutes

* 1 *** run every 1 minute from

0 1 *** execution

0 */1 * execution at every 1 hour

0 * execution at every 1 hour

2 8-20/3 *** run

30 5, ** executed at on the 1st and 15th

 

 

2.3 & background Command Execution

 

When running a job on the foreground, the terminal is occupied by the job, but it does not occupy the terminal when running the job on the background. You can run the job in the background using the & command.

 

For example:

30 2 ***/data/APP/scripts/hotbackup/hot_database_backup.sh &

 

Be careful when running jobs in the background: do not execute commands that require user interaction in the background, because your machine will be silly there.

However, running a job in the background will output the result to the screen, interfering with your work. If a job running in the background produces a large amount of output, you 'd better use the following method to redirect its output to a file:

For example:

Command> out. File 2> & 1 &

 

In this example, 2> & 1 indicates that all standard output and error output will be redirected to an out. File file.

 

2.4 2> & 1 meaning

 

Let's take a look at an example:

0 2 ***/u01/test. Sh>/dev/null 2> & 1 &

 

This statement means to execute this command in the background, redirect error output 2 to standard output 1, and then put all the standard output 1 to the/dev/null file, that is, empty.

 

Here are several numbers:

0 indicates keyboard input

1 indicates standard output

2 indicates the error output.

 

 

We can also write:

0 2 ***/u01/test. Sh>/u01/out. File & -- this is not written. The default value is 1.

0 2 ***/u01/test. Sh 1>/u01/out. File &

0 2 ***/u01/test. Sh 2>/u01/out. File &

0 2 ***/u01/test. Sh 2>/u01/out. File 2> & 1 &

 

Redirect the tesh. Sh command output to out. file, that is, the output content is not printed to the screen, but output to the out. File file.

 

2> & 1 redirects the error output to the standard output. Then, redirect the standard input to the file out. File.

& 1 indicates the description 1 of the file, indicating the standard output. If & is missing, it becomes the number 1, indicating redirection to file 1.

 

&: Background execution

 

Test:

Ls 2> 1: No errors without 2 files are reported, but an empty file 1 is output;

Ls XXX 2> 1: no error in XXX file output to 1;

Ls XXX 2> & 1: The file 1 is not generated, but the error is returned to the standard output;

Ls XXX> out.txt 2> & 1 = ls xxx 1> out.txt 2> & 1; because the redirection symbol> expiration is 1, the error output and standard output are all transmitted to the out.txt file.

 

 

2.5 2> the reason why & 1 is written below

Format: Command> file 2> & 1 = command 1> file 2> & 1

 

First, the command> file redirects the standard output to the file. 2> & 1 indicates that the standard output is copied due to a standard error, that is, it is also redirected to the file, the final result is that both the standard output and errors are redirected to the file.

 

If changed to: Command 2> & 1> File

2> & 1 the standard error copies the standard output, but the standard output is still on the terminal.> The output is redirected to the file, but the standard error is still on the terminal.

Original article: http://blog.csdn.net/tianlesoftware/article/details/5315039

Related Article

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.