crond使用

https://zhidao.baidu.com/question/1577002760551414060.html
http://www.360doc.com/content/12/1009/16/10834920_240453743.shtml
https://www.cnblogs.com/lzhp/p/6087525.html

1、系统自带,不用重启,不用管,直接写就行。

* * * * * /root/b.sh

cat /etc/crontab

[root@ceshi ~]# cat /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下的定时任务
crontab -u root -e

每天早上6点追加一条字符串到一个文本。
0 6 * * * echo "Good morning." >> /tmp/test.txt

每两个小时追加一条字符串一个文本。
0 */2 * * * echo "Have a break now." >> /tmp/test.txt

晚上11点到早上8点之间每两个小时,早上八点
0 23-7/2,8 * * * echo "Have a good dream:)" >> /tmp/test.txt

每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 11 4 * 1-3 command line

1月1日早上4点
0 4 1 1 * command line

每月每天每小时的第 0 分钟执行一次 /bin/ls
0 * * * * /bin/ls

在 12 月内, 每天的早上 6 点到 12 点中,每隔 20 分钟执行一次 /usr/bin/backup
*/20 6-12 * 12 * /usr/bin/backup

周一到周五每天下午 5:00 寄一封信给 alex_mail_name :
0 17 * * 1-5 mail -s "hi" alex_mail_name < /tmp/maildata

每月每天的午夜 0 点 20 分, 2 点 20 分, 4 点 20 分....执行 echo "haha"
20 0-23/2 * * * echo "haha"

晚上11点到早上8点之间每两个小时,早上8点,显示时间
0 23-7/2,8 * * * date

2. crontab用法

crontab –e : 修改 crontab 文件,如果文件不存在会自动创建。
crontab –l : 显示 crontab 文件。
crontab -r : 删除 crontab 文件。
crontab -ir : 删除 crontab 文件前提醒用户。

3. 流程举例

[root@ceshi ~]# cat b.sh
#!/bin/bash
#monitor available disk space
#提取本服务器的IP地址信息
IP=`ifconfig eth0 | grep "inet" | grep "netmask" |awk '{print $2}'`
PACE=`df -hP | awk '{print int($5)}' | head -n 2 | tail -n 1`
if [ "$PACE" -ge 20 ]
then
echo "$IP 服务器 磁盘空间 使用率已经超过90%,请及时处理。"|mail -s "$IP 服务器硬盘告警" [email protected]
fi


[root@www ~]# touch crontest.cron
[root@www ~]# vi test.cron
[root@www mnt]# cat crontest.cron
*/1 * * * * sh /root/a.sh

开始执行,以后每次改动脚本,都要重新执行下面命令,

[root@www mnt]# crontab crontest.cron


或者:


[root@www mnt]#crontab -e

自己打开一个文件,写入脚本就行,不用管,自动就会执行。和上面一样

猜你喜欢

转载自www.cnblogs.com/effortsing/p/10070424.html