Linux crontab timing task configuration method of details

  • 2020-05-13 04:23:04
  • OfStack

CRONTAB concepts/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 an "crontab" file for later reading and execution.

cron system scheduling process. You can use it to run jobs during off-peak load periods each day, or at different times of the week or month. cron is the primary scheduling process for the system and can run jobs without human intervention. The crontab command allows users to submit, edit, or delete jobs. Each user can have an crontab file to hold scheduling information. System administrators can disable or allow users to have their own crontab files through cron.deny and cron.allow.

1: crontab file 1 is located under /etc/, where the scheduler running the system is stored.

[root@localhost cron]# more /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

2: each user will generate 1 automatically generated crontab file, 1 normally located in /var/spool/cron directory

[root@localhost cron]# cd /var/spool/cron

[root@localhost cron]# ls

oracle root

If you use command crontab - r will delete the current user crontab files, for example, you switch to the oracle account, execute this command, so/var spool cron/oracle file will be deleted, if you want to create the file you just need to use crontab - e command. Note that normal user 1 does not normally have access to /var/spool/cron

3: cron.deny and cron.allow

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

/etc/ cron.allow represents users who can use crontab.

By default, the cron.allow file does not exist. If both files exist at the same time, /etc/ cron.allow takes precedence. If neither file exists, only the 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 21 2004 crontab

-rw-r--r-- 1 root root 0 May 16 2008 cron.deny

cron.hourly:

total 0

cron.monthly:

total 8

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

cron.weekly:

total 16

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

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

cron.daily:

total 60

-rwxr-xr-x 1 root root 286 Aug 14 2004 tmpwatch

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

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

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

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

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

-rwxr-xr-x 1 root root 121 Aug 22 2007 slocate.cron

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

cron.d:

total 12

-rw------- 1 root root 366 Jun 12 2007 sa-update

-rw-r--r-- 1 root root 188 Jul 18 2007 sysstat

CRONTAB online manual

Note: different versions of the Linux system may contain different contents in the crontab 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 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 in 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 should always use the -u option for safety'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 editor, the modified crontab will be installed
       automatically.

    -i   This option modifies the -r option to prompt the user for a
       'y/Y' 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 / replace-
       ment 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 ( ' ' POSIX'').  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)

CRONTAB grammar and application

1: view the timed task of the current user

[oracle@localhost ~]$ crontab -l

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

2: edit the timing task of the current user

You can modify, delete, and add some timed tasks in the edit state. Use # for comment 1

[oracle@localhost ~]$ crontab -e

3: delete the scheduled task of the current user

[root@localhost ~]# crontab -r

[root@localhost ~]# crontab -l

no crontab for root

4:

As shown below, the format of the timing task in the crontab file is as follows:

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

The meaning and format of the fields in each entry in the crontab file:

Column 1 min. 1 -- 59

Column 2 hour: 1-23 (0 for midnight)

Column 3:1 -- 31

Column 4:1 -- 12

Column 5: week 0 -- week 6(0 for Sunday, 1 for week 1, and so on)

Column 6 is the command to run

We use C1, C2, C3, C4, C5, C6 to represent the six columns. The first five columns are combined to determine the frequency of executing the script. The minimum frequency is once per minute. * / n; T1 - T2; a, b c; Four forms are used to represent:

When C1 is *, scripts are executed every minute, when C2 is *, programs are executed every hour, and so on...

When C1 is T1-T2, it means that it will be executed from T1 minute to T2 minute; when C2 is T1-T2, it means that it will be executed from T1 hour to T2 hour, and so on...

When C1 is */n, it means 1 time interval per n minute, */ C2 means 1 time interval per n hour, and so on...

When C1 is a, b, c... , a, b, c... Minutes to execute, C2 for a, b, c... , a, b, c... Hours to execute, and so on...

Here are some examples for your reference

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

Said 23 points every day 59 executing scripts/home/oracle/scripts/alert_log_archive sh

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

Said every 5 minutes to perform one script/home/oracle/scripts/monitoring_alert_log sh

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

Mail a letter to kerry@domain.name at 20:00 every day from Monday to Friday

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

about > /dev/null 2 > & Explanation of 1:

The 0 represents keyboard input

One is standard output

2 represents error output.

We first create the test.sh script as follows:

#! /bin/sh

echo "hello, everybody, now is " `date`

date > > test.txt

And then add the job

* * * * * /home/oracle/test.sh > /home/oracle/log.txt & The default value is 1, which corresponds to command 1 below

* * * * * /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 output of the tesh.sh command to log.txt, that is, the output is not printed to the screen, but to the log.txt file. If you need to append rather than overwrite, use > > Instead of >

2 > & 1 is to redirect the error output to standard output. Then redirect standard input to the file log.txt.

& So 1 is for file description 1, which is for standard output, if it's missing here & It becomes the number 1, which means redirection to file 1.

Matters needing attention:

There are two issues to be aware of when configuring timed tasks:

1: necessary environment variables are set in SHELL; For example, an shell script manually executes OK, but when configured for background job execution, the environment variable of ORACLE cannot be obtained. This is because of the crontab environment variable. By default, the environment of Crontab does not contain the environment of the current user in the system. Therefore, you need to add the necessary environment variable Settings in the shell script

2: try to use full path for all files, avoid using relative path.


Related articles: