Linux crontab Command Detailed __linux

Source: Internet
Author: User
Tags time 0 crontab syntax
I. Introduction to Crontab

The function of the crontab command is to schedule the execution of some commands at certain intervals. 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:

[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

/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/var/spool/cron

Table of Contents:

[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. two. Crontab Use instructions 2.1 Crontab Syntax

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. 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 added command 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 :

43 21 * * * * 21:43 execution

15 05 * * * * 05:15 execution

0 17 * * * * 17:00 execution

0 17 * * 1 per Monday 17:00 execution

0,10 17 * * 0,2,3 every Sunday, Tuesday, Wednesday 17:00 and 17:10 execution

0-10 17 1 * * * each month 1st from 17:00 to 7:10 every 1 minutes to perform

0 0 1,15 * 1 each month 1st and 15th and day 0:00 execution

42 4 1 * * * every month, 1st, 4:42 minutes of execution

0 21 * * 1-6 weeks one to Saturday 21:00 execution

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

*/10 * * * * * * every 10 minutes

* 1 * * * * from 1:0 to 1:59 every 1 minutes

0 1 * * * * 1:00 execution

0 */1 * * * * * * each time 0 minutes every 1 hours to perform

0 * * * * * * each time 0 minutes every 1 hours to perform

2 8-20/3 * * * * 8:02,11:02,14:02,17:02,20:02 execution

30 5 1,15 * * 1st and 15th 5:30 Executive 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: 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: 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

First look at an example: 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:

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; testing:

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, and the standard output is copied, 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;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.

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.