- crontab定时器
- 简介:Linux下的定时任务
- 1、==vi /etc/crontab==
- 2、==tail -f==:监控文件里面新增数据的情况(用来监控实时变化的文件)
- 3、==tail -f /var/log/cron==:查看crontab的日志监控文件
- <1>service crond status:查看cron服务状态
- <2>service crond start:启动cron服务
- <3>tail -f /var/log/cron:查看crontab的日志监控文件
- 4、问题:如果设置每5分支执行,怎么写定时器脚本
(1)先在克隆会话下写一个脚本,vi 一个x.sh脚本(随意叫啥),echo 一个hello,追加重定向文件夹
[root@hadoop61 ~]# cd /usr/local/shellDemo [root@hadoop61 shellDemo]# ll 总用量 36 -rw-r--r--. 1 root root 125 10月 14 16:34 a.txt -rw-r--r--. 1 root root 60 10月 12 16:53 b.sh -rw-r--r--. 1 root root 39 10月 12 16:37 c.sh -rwxr-xr-x. 1 root root 53 10月 10 11:26 demo.sh -rw-r--r--. 1 root root 46 10月 12 16:55 d.sh -rw-r--r--. 1 root root 48 10月 12 20:17 for.sh -rw-r--r--. 1 root root 62 10月 12 21:45 if.sh -rw-------. 1 root root 0 10月 14 11:17 nohup.out -rw-r--r--. 1 root root 47 10月 14 11:26 while2.sh -rw-r--r--. 1 root root 45 10月 12 20:59 while.sh [root@hadoop61 shellDemo]# vi x.sh #!/bin/bash echo "hello" >> /usr/local/shellDemo/hello.txt
(2)给脚本赋一个执行权限
[root@hadoop61 shellDemo]# chmod +x x.sh [root@hadoop61 shellDemo]#
(3)执行vi /etc/crontab命令
执行:
[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
注:
<1>前面#都是注释
<2>可以看到5个*号,从左到右分别代表分、时、日、月、星期几
(4)在下面一行写一个定时器脚本
格式:全写*意味着每分钟执行一次,每一分钟的零分开始执行
bin/sh 路径固定不动,后面跟命令的路径
注:用户名必须得写,不写就报错了(下面的用户名是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.sh2、tail -f:监控文件里面新增数据的情况(用来监控实时变化的文件)
(1)注:出现下面这种情况,说明文件还没开始执行,因为是从每一分钟的零分开始执行
[root@hadoop61 shellDemo]# tail -f hello.txt tail: 无法打开"hello.txt" 读取数据: 没有那个文件或目录 tail: 没有剩余文件
(2)再查一次就发现有了
[root@hadoop61 shellDemo]# tail -f hello.txt hello
(3)查文件执行的最新的1行和最新的三行
[root@hadoop61 shellDemo]# tail -1 hello.txt hello [root@hadoop61 shellDemo]# tail -3 hello.txt hello hello hello3、tail -f /var/log/cron:查看crontab的日志监控文件
注: 必须打开rsyslog服务cron文件中才会有执行日志(service rsyslog status)
<1>service crond status:查看cron服务状态执行:可以看到running已开启
[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 四 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:启动cron服务
<3>tail -f /var/log/cron:查看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
注:从上表这些监控文件里面提取一则可以看到
Oct 14 17:56:01 hadoop61 CROND[6400]: (root) CMD (/bin/sh /usr/local/shellDemo/x.sh)
(1)谁执行的:root
(2)命令是什么:/bin/sh /usr/local/shellDemo/x.sh
(1)找到minute对应的 *,然后写成 */5 ,就是每5分钟执行一次
# 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
注:
(1)可以写多行任务,多行任务间没啥关系
(2)任务不想用了可以加#注释掉
#*/5 * * * * root /bin/sh /usr/local/shellDemo/x.sh
(3)每天上午九点开始执行可以写成
0 9 * * * root /bin/sh /usr/local/shellDemo/x.sh



