linux的crontab绝对路径咋填,RHCE培训视频Crontab详解(创想云教育)

原标题:RHCE培训视频Crontab详解(创想云教育)

RHCE8培训视频Crontab详解

Linux 定时任务服务 crond (crontab 服务)

附课堂笔记:

[root@servera ~]# systemctl status crond

● crond.service - Command Scheduler

Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enable>

Active: active (running) since Sun 2020-11-22 09:12:19 CST; 5h 31min ago

Main PID: 739 (crond)

为什么需要定时任务服务crond 1 7*24 开机对外提供服务

重要数据:rsync tar

定时任务加 SHELL 脚本来实现自化备份

Linux 系统中定时任务分类:

1 系统自身的定时任务 7 段

2 用户执行的定时任务 6 段

Crontab 在固定时间间隔执行指定的脚本或命令

[root@servera ~]# vim /etc/crontab # 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@servera ~]# whatis crontab

crontab (1) - maintains crontab files for individual users

crontab (1p) - schedule periodic background work crontab (5) - files used to schedule the execution of programs

-u define user

-e edit user's crontab

-l list user's crontab

-r delete user's crontab

定时任务的格式:

用户执行的定时任务分为 6 段,每个段通过空格进行分隔

系统定时任务分为 7 段,空格进行分隔

30 22 * * * /scripts/memory.sh 前五段时间设定 第六段要执行的命令或者脚本

特殊符号:

* 号表示任意时间都

例如:小时使用*号等价于 0 – 23 分钟* 等价于 0 – 59

- 减号,表示分隔符,表示一个时间范围

30 12-16 * * * /scripts/memory.sh

,逗号,表示分隔时间段

30 12,16,19 * * * /scripts/memory.sh

/n n 代表数字,每隔 n 个单位时间 例如:每 10 分钟执行一次任务

*/10 * * * * 等价于 0 – 59/10 * * * *

30 3,12 * * * /scripts/memory.sh

30 21 * * * /scripts/memory.sh 45 4 1,10,22 /scripts/memory.sh 10 1 * * 6,0 /scripts/memory.sh

* 23,00-07/1 * * * /scripts/memory.sh 不规范写法

0 23,00-07/1 * * * /scripts/memory.sh

总结:

[root@servera ~]# crontab -u root -l 错误

* * * * * tar czvf /tmp/$(date +%F).etc.tar.gz /etc

crontab %号有特殊的作用,命令使用了%百分号,换行符Nov 22 02:01:01 jegui CROND[4783]: (root) CMD (tar czvf

/tmp/$(date +) date 格式化参数会被截掉,导致语法错误

[root@servera ~]# crontab -u root -l

* * * * * tar czvf /tmp/$(date +\%F).etc.tar.gz /etc

[root@servera scripts]# ll /tmp/20*

ls: cannot access '/tmp/20*': No such file or directory [root@servera scripts]# crontab -l -u root

* * * * * /scripts/backup.sh [root@servera scripts]# ll backup.sh

-rw-r--r--. 1 root root 55 Nov 22 15:08 backup.sh

[root@servera scripts]# crontab -l -u root

* * * * * /bin/sh /scripts/backup.sh

[root@servera scripts]# crontab -l

* * * * * /bin/sh /scripts/backup.sh &> /dev/null

[root@servera ~]# crontab -u root -l

* * * * * /bin/sh /scripts/backup.sh

[root@servera clientmqueue]# rm –rf * 如果文件太多,占用空间大, 删除非常非常慢 /bin/rm argument list too long 错误[root@servera clientmqueue]# ls |xargs rm –f

总结:

1、定时任务的命令或程序最好写到脚本里执行

2、执行shell 脚本任务前加/bin/bash 或 /bin/sh 注解: 脚本没有执行权限, 可以执行

3、定时任务命令或脚本结尾添加 &> /dev/null 字符设备文件

注意:定时执行的程序有输出或错误,输出内容会以邮件形式发送给 cron 用户,如果 sendmail 没有启动产生大量文件,消耗掉大量 inode,导致系统报错No space left on device

4、定时任务脚本路径写绝对路径

调试定时任务:

1 调整系统的时间 systemctl restart crond

通过 crontab 实现秒级定时任务[root@servera scripts]# cat demo.sh #!/bin/bash

step=1

for (( i=0; i<60; i=(i+step) ));do

$(date >> /mnt/file) sleep $step

done exit 0

实现定时任务秒级

[root@servera scripts]# cat backup.sh #!/bin/bash

while true;do

$(date >> /tmp/cloudshell) sleep 1

done

[root@servera ~]# at 19:30

warning: commands will be executed using /bin/sh at> yum update --security -y

at>

job 2 at Sun Nov 22 19:30:00 2020 [root@servera ~]# at -l

2 Sun Nov 22 19:30:00 2020 a root

错误: No space left on device df –TH 磁盘还有非常大的空间一个文件存储到文件系统中,至少要占用一个 inode 和一个 block

关于更多视频资料,请访问:

https://www.cloudshelledu.com/redhat返回搜狐,查看更多

责任编辑:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值