shell9: use of crontab timer

crontab timer

Introduction: scheduled tasks under Linux

1,vi /etc/crontab

(1) First, write a script under the clone session, vi an x.sh script (whatever it is called), echo a hello, and add a redirect folder

[root@hadoop61 ~]# cd /usr/local/shellDemo
[root@hadoop61 shellDemo]# ll
 Total consumption 36
-rw-r--r--. 1 root root 125 10 June 14-16:34 a.txt
-rw-r--r--. 1 root root  60 10 December 16:53 b.sh
-rw-r--r--. 1 root root  39 10 December 16:37 c.sh
-rwxr-xr-x. 1 root root  53 10 October 11:26 demo.sh
-rw-r--r--. 1 root root  46 10 December 16:55 d.sh
-rw-r--r--. 1 root root  48 10 December 20:17 for.sh
-rw-r--r--. 1 root root  62 10 December 21:45 if.sh
-rw-------. 1 root root   0 10 November 14:17 nohup.out
-rw-r--r--. 1 root root  47 10 November 14:26 while2.sh
-rw-r--r--. 1 root root  45 10 December 20:59 while.sh
[root@hadoop61 shellDemo]# vi x.sh

#!/bin/bash
echo "hello" >> /usr/local/shellDemo/hello.txt

(2) Assign an execution permission to the script

[root@hadoop61 shellDemo]# chmod +x x.sh 
[root@hadoop61 shellDemo]# 

(3) Execute the vi /etc/crontab command
Execution:

[root@hadoop61 shellDemo]# vi /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 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

Note:
<1> The front # is full of notes
<2> You can see five * signs, which represent minute, hour, day, month and day of the week from left to right

(4) Write a timer script on the next line
Format: write all * means to execute once every minute, starting from zero minute
The bin/sh path is fixed, followed by the command path
Note: the user name must be written. If it is not written, an error will be reported (the following user name is root)

[root@hadoop61 shellDemo]# vi /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 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
#* * * * * root /bin/sh /usr/local/shellDemo/x.sh
* * * * * root bin/sh /usr/local/shellDemo/x.sh

2. tail -f: monitor the new data in the file (used to monitor the real-time changing file)

(1) Note: in the following case, the document has not been executed yet, because it is executed from zero every minute

[root@hadoop61 shellDemo]# tail -f hello.txt
tail: Cannot open"hello.txt" Read data: There is no such file or directory
tail: No files left

(2) Check it again and you'll find it

[root@hadoop61 shellDemo]# tail -f hello.txt 
hello

(3) Check the latest 1 line and the latest 3 lines of the file execution

[root@hadoop61 shellDemo]# tail -1 hello.txt 
hello
[root@hadoop61 shellDemo]# tail -3 hello.txt 
hello
hello
hello

3. tail -f /var/log/cron: view the log monitoring file of crontab

Note: the rsyslog service must be opened before the execution log (service rsyslog status) can be found in the cron file

<1> Service crond status: View cron service status

Execute: you can see that running is enabled

[root@hadoop61 shellDemo]# service crond status
Redirecting to /bin/systemctl status  crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since IV 2021-10-14 07:57:43 CST; 12h ago
 Main PID: 641 (crond)
   CGroup: /system.slice/crond.service
           └─641 /usr/sbin/crond -n

<2> Service crond start: starts the cron service

<3> Tail - F / var / log / cron: view the log monitoring file of crontab

[root@hadoop61 shellDemo]# tail -f /var/log/cron
Oct 14 17:56:01 hadoop61 CROND[6400]: (root) CMD (/bin/sh /usr/local/shellDemo/x.sh)
Oct 14 17:57:01 hadoop61 CROND[6404]: (root) CMD (/bin/sh /usr/local/shellDemo/x.sh)
Oct 14 17:58:01 hadoop61 CROND[6408]: (root) CMD (/bin/sh /usr/local/shellDemo/x.sh)
Oct 14 17:59:01 hadoop61 CROND[6413]: (root) CMD (/bin/sh /usr/local/shellDemo/x.sh)
Oct 14 18:00:01 hadoop61 CROND[6419]: (root) CMD (/bin/sh /usr/local/shellDemo/x.sh)
Oct 14 18:01:01 hadoop61 CROND[6426]: (root) CMD (/bin/sh /usr/local/shellDemo/x.sh)
Oct 14 18:01:01 hadoop61 CROND[6427]: (root) CMD (run-parts /etc/cron.hourly)
Oct 14 18:01:01 hadoop61 run-parts(/etc/cron.hourly)[6427]: starting 0anacron

Note: it can be seen from one of these monitoring files in the above table

Oct 14 17:56:01 hadoop61 CROND[6400]: (root) CMD (/bin/sh /usr/local/shellDemo/x.sh)

(1) Who executed: root
(2) What is the command: / bin/sh /usr/local/shellDemo/x.sh

4. Question: how to write timer script if it is set to execute every 5 branches

(1) Find the * corresponding to minute and write it as * / 5, which is executed every 5 minutes

# 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
*/5 * * * * root /bin/sh /usr/local/shellDemo/x.sh

Note:
(1) You can write multiline tasks. There is no relationship between multiline tasks
(2) If you don't want to use the task, you can add # comments

#*/5 * * * * root /bin/sh /usr/local/shellDemo/x.sh

(3) It starts at 9 a.m. every day

0 9 * * * root /bin/sh /usr/local/shellDemo/x.sh

Tags: Linux Operation & Maintenance vim

Posted on Thu, 14 Oct 2021 16:38:51 -0400 by [PQ3]RogeR