Use the Linux command crontab interval time to perform other commands _linux

Source: Internet
Author: User

1.1/etc/crontab file

In the/etc directory there is a crontab file, where there are some scheduling programs running on the system. Each user can establish their own scheduling crontab.

Such as:

Copy Code code as follows:

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

Shell=/bin/bash

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

Mailto=root

home=/

# Run-parts

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

4 * * * Root run-parts/etc/cron.daily

4 * * 0 root run-parts/etc/cron.weekly

4 1 * * Root run-parts/etc/cron.monthly



1.2/etc/cron.deny and/etc/cron.allow files
Copy Code code as follows:

/etc/cron.deny represents a user who cannot use the crontab command

/etc/cron.allow represents a user who can use crontab.



If two files exist at the same time, then/etc/cron.allow first. If two files do not exist, only a superuser can schedule the job.

Each user will generate a crontab file of their own. These files are in the/var/spool/cron directory, such as:

Copy Code code as follows:

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

[Root@dave cron]# ls

Oracle Root



We look at the file directly and the contents are consistent with the crontab-l displayed by the corresponding user.
Copy Code code as follows:

[Root@dave cron]# Cat Oracle

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

[Root@dave cron]# Cat Root

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

[Root@dave cron]#



Two. Crontab Use instructions

2.1 Crontab Grammar

Copy Code code as follows:

Usage:crontab [-u user] File

crontab [-u user] [-e |-l |-r]

-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)

Where file is the name of the command file. If the file is specified on the command line, then the crontab command is executed, and the file is copied to the Crontabs directory, and if the file is not made on the command line, the crontab command accepts the command typed on the standard input (keyboard). And they are also stored in the crontab directory.

Help:

Copy Code code as follows:

[Root@dave ~]# Mans 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]

OPTIONS

-U It Specifies the name of the user whose crontab is 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 this if you are running inside of SU (8) Your should always use the-u option for  Safety¡¯s sake. The "This" is "used to install a" new crontab from some named file or standard input if the Pseudo-filen Ame "-" is given.

-L The current crontab is displayed on standard output.

-R The current crontab would be removed.

E-This option is used to edit the current crontab using the editor specified by the VISUAL or editor environment VA  Riables. After your exit from the Edi-tor, the modified crontab would be installed automatically.

I-option modifies the-r option to prompt the user for a¡¯y/y¡¯response before actually removing the crontab .

-S It'll append the current SELinux security context string as a mls_level setting to the crontab file before Editing/replacement Occurs-see The documentation of Mls_level in Crontab (5).


Copy Code code as follows:

ALSO

Crontab (5), cron (8)

FILES

/etc/cron.allow

/etc/cron.deny

Standards

The crontab command conforms to IEEE std1003.2-1992 (¡®¡®posix¡¯¡¯). This is the new command syntax differs from previous versions of Vixie Cron, as as is from the classic

SVR3 syntax.

Diagnostics

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

AUTHOR

Paul Vixie <vixie@isc.org>

4th Berkeley Distribution16 Januar 2007 CRONTAB (1)

2.2 Crontab Format Description

We can use CRONTAB-E to add a command to execute. The results of the command execution, both standard output and error output, are sent to the user in the form of a message.

The commands you add must be in the following format:

* * * */command path
The first five fields can take an integer value, specify when to start work, and the sixth field is a string, which is the command field, which includes the command that Crontab Dispatch executes. Separate fields with spaces and tabs.

The first 5 fields represent:

Minutes: 0-59

Hours: 1-23

Date: 1-31

Month: 1-12

Week: 0-6 (0 for Sunday)

You can also use some special symbols:

*: Express any time

,: Represents a split

-: Represents a segment, such as the second end: 1-5, which means 1 to 5 points

/n: The unit that represents each n is executed once, as in the second paragraph, */1, which means that the command is executed every 1 hours. Can also be written as 1-23/1.


Some examples:

Copy Code code as follows:

8,12,16 * * */data/app/scripts/monitor/df.sh

2 * * */data/app/scripts/hotbackup/hot_database_backup.sh

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

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

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



Copy Code code as follows:

43 21 * * * * 21:43 execution

15 05 * * * * 05:15 execution

0 17 * * * * 17:00 execution

2.3 & Background Execution command

When a job is run in the foreground, the terminal is occupied by the job, and when the job is run in the background, it does not occupy the terminal. You can use the & command to put the job in the background.

Such as:

Copy Code code as follows:

2 * * */data/app/scripts/hotbackup/hot_database_backup.sh &


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

However, running the job in the background will output the results to the screen, interfering with your work. If a job that runs in the background produces a large amount of output, it is best to redirect its output to a file using the following method:

Such as:

Copy Code code as follows:

Command >out.file 2>&1 &

In this example, 2>&1 indicates that all standard output and error output will be redirected to a file called Out.file.

2.4 2>&1 Meaning

Let's look at one example:

Copy Code code as follows:

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


The meaning of this sentence is to execute this command in the background, redirect the error output 2 to standard output 1, and then put the standard output 1 all to the/dev/null file, that is, empty.

Here are a few numbers that mean:

0 indicates keyboard input

1 indicates standard output

2 indicates an error output.


We can also write this:

Copy Code code as follows:

0 2 * * */u01/test.sh >/u01/out.file &--not written here, default 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 &

Redirects the tesh.sh command output to the Out.file, where the output is not printed to the screen but is output to the Out.file file.

2>&1 is to redirect the error output to standard output. The standard input is then redirected to the file out.file.

&1 represents the file Description 1, which represents the standard output, and if the & is less than the number 1, it means redirecting to file 1.

&: Backstage Execution

Test:

Copy Code code as follows:

LS 2>1: does not report No 2 file error, but will output an empty file 1;

ls xxx 2>1: no xxx This file error output to 1;

ls XXX 2>&1: will not generate 1 of this file, but the error ran to the standard output;

ls xxx >out.txt 2>&1 = = ls xxx 1>out.txt 2>&1; because the redirection symbol > default is 1, this sentence uploads the error output and standard output to the OUT.txt file.



2.5 2>&1 wrote in the back of the reason

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

First, command > file redirects standard output to file, 2>&1 is the standard error copy of standard output, which is also redirected to file, and the final result is that standard output and errors are redirected to file.

If changed to: command 2>&1 >file

The 2>&1 standard error copies the behavior of the standard output, but at this point the standard output is still in the terminal. The output is redirected to file after >file, but the standard error remains at the terminal.

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.