Detailed explanation of crontab task cycle plan under CentOS

Time:2020-3-25

Crontab task cycle plan

Format:

* * * * *  command

Minute (0-59), hour (0-23), day (1-31), month (1-12), week (0-6)

Explain:

1. Asterisk matches all characters

2. An integer, must match exactly

3. Two integers separated by dashes that match the range of values.

4. The time range is followed by a ramp and a step value. 1-10/2

General user process:

Crontab – e crontab is set with setuid, so the user has his permission root, which will be saved in / var / spool / cron / $username after file processing.

Journal:

/var/log/cron

Configuration directory:

/Var / run / crond.pid process number address

/Etc / crontab instruction file

/Etc / $weekday. And so on. That is to say, you can put the script in it and it will execute on time.

/Etc / cron. {deny, allow} rejects and allows users to use crontab

Vixie-Cron:

This package is installed by default when CentOS is installed. This package can include a step value. That is, 1,3,6,9,12 can be written as 1-12 / 3 divided by 3. The so-called * / 1 * / 2 is to run command every minute and every two minutes. That is, 1-59 / {2,3}.

%Application of No

%The percent sign indicates a line break in the command. Only the text before the first percent sign is included in the actual command. The rest of the lines are the standard input of the command.

Installation:

yum install -y crontabs

Use:

crontab –help

Crontab – e ා check out a copy of crontab, call VI to edit it, and save it in the crontab directory again.

Crontab – L – prints the contents of crontab to standard output.

The crontab – R volume does not contain any content.

……

The root directory includes other users.

crontab -r $username

[[email protected] ~]# crontab –help

crontab: invalid option — ‘-‘

crontab: usage error: unrecognized option

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)

[[email protected] ~]# ll /usr/bin/crontab

-rwsr-xr-x 1 root root 47520 Jul 19  2011 /usr/bin/crontab

[[email protected] ~]# cat /var/run/crond.pid

2074

[[email protected] ~]# ll /etc/ |grep -E cron

drwxr-xr-x   2 root root    4096 Apr  2 00:59 cron.d

drwxr-xr-x.  2 root root    4096 Sep 27  2011 cron.daily

-rw-r–r–   1 root root       0 Jul 19  2011 cron.deny

drwxr-xr-x   2 root root    4096 Sep 27  2011 cron.hourly

drwxr-xr-x   2 root root    4096 Sep 27  2011 cron.monthly

-rw-r–r–   1 root root     457 Sep 27  2011 crontab

drwxr-xr-x   2 root root    4096 Sep 27  2011 cron.weekly

[[email protected] ~]# cat /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

# For details see man 4 crontabs

# Example of job definition:

# .—————- minute (0 – 59)

# |  .————- hour (0 – 23)

# |  |  .———- day of month (1 – 31)

# |  |  |  .——- month (1 – 12) OR jan,feb,mar,apr …

# |  |  |  |  .—- day of week (0 – 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

# |  |  |  |  |

# *  *  *  *  * user-name command to be executed

[[email protected] ~]# ll /var/spool/cron/

total 8

-rw——- 1 root   root   60 Apr  2 01:33 root

-rw——- 1 zwhset zwhset 68 Apr  2 01:51 zwhset

[[email protected] ~]# tail -5 /var/log/cron

Apr  2 17:24:01 nagios CROND[20544]: (root) CMD (/bin/sh ~/hello)

Apr  2 17:25:01 nagios CROND[20549]: (root) CMD (/bin/sh ~/hello)

Apr  2 17:25:01 nagios CROND[20550]: (root) CMD (/bin/sh ~/hello)

Apr  2 17:25:01 nagios CROND[20551]: (zwhset) CMD (echo “hello” >> ~/1.txt)

Apr  2 17:25:01 nagios CROND[20552]: (zwhset) CMD (echo “hello” >> ~/1.txt)

[[email protected] ~]# ps aux |grep cron

root      2074  0.0  0.5 117212  1316 ?        Ss   01:40   0:04 crond

root     20641  0.0  0.6 139684  1468 pts/1    S+   17:30   0:00 crontab -e

zwhset   20642  0.0  0.7 119284  1756 pts/1    S+   17:30   0:00 /bin/vi /tmp/crontab.YphQpH

root     20663  0.0  0.3 103236   856 pts/0    S+   17:31   0:00 grep cron

[[email protected] ~]# lsof -c crontab

COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF   NODE NAME

crontab 20641 root  cwd    DIR              253,0     4096 130818 /root

crontab 20641 root  rtd    DIR              253,0     4096      2 /

crontab 20641 root  txt    REG              253,0    47520 401259 /usr/bin/crontab

crontab 20641 root  mem    REG              253,0     5952 261889 /lib64/security/pam_deny.so

crontab 20641 root  mem    REG              253,0    18592 261891 /lib64/security/pam_env.so

crontab 20641 root  mem    REG              253,0    18592 261903 /lib64/security/pam_limits.so

crontab 20641 root  mem    REG              253,0    10224 261901 /lib64/security/pam_keyinit.so

crontab 20641 root  mem    REG              253,0    10240 261906 /lib64/security/pam_loginuid.so

crontab 20641 root  mem    REG              253,0     6040 261912 /lib64/security/pam_permit.so

crontab 20641 root  mem    REG              253,0    14384 261923 /lib64/security/pam_succeed_if.so

crontab 20641 root  mem    REG              253,0    10200 261905 /lib64/security/pam_localuser.so

crontab 20641 root  mem    REG              253,0    51952 261929 /lib64/security/pam_unix.so

crontab 20641 root  mem    REG              253,0   113432 261657 /lib64/libnsl-2.12.so

crontab 20641 root  mem    REG              253,0    14456 261884 /lib64/security/pam_access.so

crontab 20641 root  mem    REG              253,0    65928 261663 /lib64/libnss_files-2.12.so

crontab 20641 root  mem    REG              253,0 99158576 397021 /usr/lib/locale/locale-archive

crontab 20641 root  mem    REG              253,0   383504 261640 /lib64/libfreebl3.so

crontab 20641 root  mem    REG              253,0    40400 261651 /lib64/libcrypt-2.12.so

crontab 20641 root  mem    REG              253,0  1916568 261647 /lib64/libc-2.12.so

crontab 20641 root  mem    REG              253,0   113096 261709 /lib64/libaudit.so.1.0.0

crontab 20641 root  mem    REG              253,0    19536 261653 /lib64/libdl-2.12.so

crontab 20641 root  mem    REG              253,0    55848 261879 /lib64/libpam.so.0.82.2

crontab 20641 root  mem    REG              253,0   122040 261718 /lib64/libselinux.so.1

crontab 20641 root  mem    REG              253,0   154464 261636 /lib64/ld-2.12.so

crontab 20641 root    0u   CHR              136,1      0t0      4 /dev/pts/1

crontab 20641 root    1u   CHR              136,1      0t0      4 /dev/pts/1

crontab 20641 root    2u   CHR              136,1      0t0      4 /dev/pts/1

crontab 20641 root    3u  unix 0xffff88000225c380      0t0  91831 socket

crontab 20641 root    5u   REG              253,0       68 916745 /tmp/crontab.YphQpH