Linux crontab Timing Task Configuration method (detailed) _linux

Source: Internet
Author: User

Crontab Concept/Introduction

The crontab command is used to set instructions that are periodically executed. This command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution.

Cron system scheduling process. You can use it to run a job on a daily off-peak load time period, or at different times of the week or January. Cron is the primary scheduling process of a system and can run jobs without human intervention. The crontab command allows the user to submit, edit, or delete the appropriate job. Each user can have a crontab file to save scheduling information. System administrators can disable or allow users to have their own crontab files through the Cron.deny and cron.allow two files.

1:crontab files are generally located under/etc/, which stores the scheduler that the system is running in.

[Root@localhost cron]# More/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

2: Each user will generate an automatic generation of their own crontab files, generally located in the/var/spool/cron directory

[Root@localhost cron]# Cd/var/spool/cron

[Root@localhost cron]# ls

Oracle Root

If you use the command crontab-r will delete the current user's crontab file, such as you switch to the Oracle account, execute the command, then/var/spool/cron/oracle file will be deleted, if you want to create the file only need to use crontab- e command. Note that ordinary users generally do not have permission to access/var/spool/cron

3:cron.deny and 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.

By default, the Cron.allow file does not exist. 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.

But what makes you wonder is that the Cron.deny file is empty, as shown below

[Oracle@localhost etc]$ more Cron.deny

4:cron.hourly,cron.daily,cron.monthly,cron.weekly

[Oracle@localhost etc]$ LS-LRT cron*

-rw-r--r--1 root root 255 Sep crontab

-rw-r--r--1 Root 0 2008 Cron.deny

Cron.hourly:

Total 0

Cron.monthly:

Total 8

-rwxr-xr-x 1 root 278 Sep 0anacron

Cron.weekly:

Total 16

-rwxr-xr-x 1 root 277 Sep 0anacron

-rwxr-xr-x 1 root root 414 June 6 2007 00-makewhatis.cron

Cron.daily:

Total 60

-rwxr-xr-x 1 root root 286 Aug Tmpwatch

-rwxr-xr-x 1 root 276 Sep 0anacron

-rwxr-xr-x 1 root root 2133 Dec 1 prelink

-rwxr-xr-x 1 root 180 May 2 2006 Logrotate

-rwxr-xr-x 1 root root 418 June 6 2007 00-makewhatis.cron

-rwxr-xr-x 1 root Aug 2 2007 rpm

-rwxr-xr-x 1 root root 121 Aug 2007 Slocate.cron

lrwxrwxrwx 1 root May 2008 00-logwatch->. /log.d/scripts/logwatch.pl

CRON.D:

Total 12

-RW-------1 root root 366 June 2007 Sa-update

-rw-r--r--1 root root 188 2007 Sysstat

Crontab Online Handbook

Note: Different versions of the Linux system may crontab the contents of the manual, please refer to the actual version.

[Oracle@localhost ~]$ man crontab | More

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 of the tables used to drive the cron (8) daemon in ISC cron. Each user can have their own crontab, and though this are files in/var/spool/, they are not intended to be edit Ed directly. For SELinux in MLS mode can is even more crontabs-for each range.

    For more SELinux (8). If The Cron.allow file exists, then you must is listed the to the "to" to "use" this command. If The Cron.allow file does not exist but the Cron.deny file does exist, then to you must is not being listed in the CRON.D The Eny file in order to use this command.


If Neither of these files exists, only the "super" user would be allowed to use this command. Options-u It Specifies the name of The user whose crontab is tweaked. If This option isn't 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) to 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 pseud

    O-filename "-" 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  Variables.

    After your exit from the editor, the modified crontab would be installed automatically. I-option modifies the-r option to prompt the user for a ' y/y ' response before actually the removing B. s It would append theCurrent SELinux security context string as a mls_level setting to the crontab file before editing/replace-

ment Occurs-see The documentation of Mls_level in Crontab (5).  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.

Diagnostics a fairly informative usage message appears if your run it with A bad command line. AUTHOR Paul Vixie <vixie@isc.org> 4th Berkeley Distribution Januar 2007 CRONTAB (1)

Crontab Grammar and its application

1: View the current user's timed task

[Oracle@localhost ~]$ Crontab-l

* * * * * * */home/oracle/test.sh >/dev/null 2>&1

2: Edit the current user's timed task

You can modify, delete, and add some scheduled tasks in the editing state. Note General Use #

[Oracle@localhost ~]$ Crontab-e

3: Delete the current user's scheduled task

[Root@localhost ~]# Crontab-r

[Root@localhost ~]# Crontab-l

No crontab for Root

4:

As shown below, the scheduled task format in the general Crontab file is as follows:

* * * */home/oracle/scripts/alert_log_archive.sh >/dev/null 2>&1

Crontab the meaning and format of each field in each entry in the file:

First column minutes: 1--59

Second column hour: 1--23 (0 means midnight)

Third Liege: 1--31

Fourth List month: 1--12

Fifth week: Week 0--6 (0 for Sunday, 1 for Monday, etc.)

Sixth column the command to run

We will use C1, C2, C3, C4, C5, C6 to represent these six columns, the first five columns to determine the frequency of the execution of the script, the minimum frequency is executed every minute, where CN can use *; */n; T1-t2; A,b,c; Four forms to represent:

When C1 is *, the script is executed every minute, C2 is * to execute the program every hour, and so on.

When C1 is t1-t2 to be executed from the T1 minute to the T2 minute, C2 is t1-t2 to execute from T1 to T2 hours, and so on.

When C1 is */n, the interval is executed once every n minutes, C2 is performed once every n-hour interval for */n, and so on.

When C1 is a, B, C,... The first A, B, C,... Minutes to execute, C2 for a, B, C,... The first is a, B, c ... An hour to execute, and so on ....

Here are a few examples for your reference

1:59 * * */home/oracle/scripts/alert_log_archive.sh >/dev/null 2>&1

Indicates that the script is executed 23:59 every day/home/oracle/scripts/alert_log_archive.sh

2: */5 * * * */home/oracle/scripts/monitoring_alert_log.sh >/dev/null 2>&1

Indicates that script is executed every 5 minutes/home/oracle/scripts/monitoring_alert_log.sh

3:0 * * 1-5 mail-s "**********" Kerry@domain.name </tmp/maildata

Send a letter to Kerry@domain.name from Monday to Friday every 20:00

..............................................

An explanation of >/dev/null 2>&1:

0 indicates keyboard input

1 indicates standard output

2 indicates an error output.

We first create the test.sh script as follows:

#! /bin/sh

echo "Hello, everybody, now is" ' Date '

Date >> Test.txt

Then add the job

* * * * * * */home/oracle/test.sh >/home/oracle/log.txt & Default value is 1, which is consistent with the following command

* * * * * * */home/oracle/test.sh 1>/home/oracle/log.txt &

* * * * * * */home/oracle/test.sh 2>/home/oracle/log.txt &

* * * * * * */home/oracle/test.sh 2>/home/oracle/log.txt 2>&1 &

1,2 redirects the tesh.sh command output to the Log.txt, where the output is not printed to the screen but is output to the Log.txt file. If you need to append rather than overwrite, you can use >> instead of >

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

&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.

Precautions:

When configuring a timed task, you need to be aware of two issues:

1: Set the necessary environment variables in the shell; For example, a shell script performs the manual OK, but when configured to perform in a background job, the Oracle's environment variable is not obtained because of the CRONTAB environment variable problem, The crontab environment does not contain the environment of the current user in the system by default. So, you need to add the necessary settings for the environment variable in the shell script

2: As far as possible all the files are full path, avoid using relative path.

The above Linux crontab Timing Task Configuration method (detailed) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.