23使用crond设置定时任务

1. 使用Linux 的 crond 执行自动任务

​ crontab 命令用于设置周期性被执行的指令。该命令从标准输入设备读取指令,并将其存放于“ crontab ”文件中,以供之后读取和执行。

1.1. crntab 服务启动与关闭
/etc/init.d/crond stop           --关闭服务
/etc/init.d/crond start           --启动服务
/etc/init.d/crond restart        --重启服务
/etc/init.d/crond reload        --重新载入配置
/etc/init.d/crond status          --查看状态
2. contab 使用流程
#1.查看 crontab 文件
[root@bat3963 fc_workspace]# crontab -l
# test task
* * * * * /root/fc_workspace/test.sh
* * * * * /root/fc_workspace/test_hello.sh
# rmove all logs and core file at every monday 00:00:00
0 0 * * 1 /root/fc_workspace/rmAllLogsAndCoreFiles.sh >> /root/fc_workspace/clearning_log
#2.修改 crontab 文件,使用 crontab -e 则可以进行 crontab 文件的编辑(最好加上注释)
[root@bat3963 fc_workspace]# crontab -e
      1 # test task
      2 * * * * * /root/fc_workspace/test.sh
      3 * * * * * /root/fc_workspace/test_hello.sh
      4 # rmove all logs and core file at every monday 00:00:00
      5 0 0 * * 1 /root/fc_workspace/rmAllLogsAndCoreFiles.sh >> /root/fc_workspace/clearning_log
#3.删除 crontab 文件
[root@bat3963 fc_workspace]# crontab -r
#4.如果要让定时任务中的脚步直接执行,需要使用给脚步赋予执行权限,也可以在crontab中使用 sh 运行脚步,此时则不需要赋予执行权限
[root@bat3963 fc_workspace]# chmod +x rmAllLogsAndCoreFiles.sh
3. 清理日志和core文件脚本
#!/bin/bash
#rmAllLogsAndCoreFiles.sh 

# 这句很重要,可以理解为引入环境变量
source /root/.bash_profile

# 定义current_time 变量,格式化时间
current_time=`date +"%Y-%m-%d %H:%M:%S"`

# $current_time,获取变量值
echo 'work time:'  $current_time

echo 'show current disk space...'
df -h
echo 'start clearning...'
echo 'remove o4basr logs'
rm -rvf /home/o4bar/workspace/log/*
echo 'remove o4mc logs'
rm -rvf /home/o4mc/workspace/log/*
echo 'remove o4jc logs'
rm -rvf /home/o4jc/workspace/log/*  
rm -rvf /home/o4jc/workspace/dlog/*
echo 'remove trade logs'
rm -rvf /home/trade/workspace/log/*
rm -rvf /home/trade/workspace/cloud_dlog/*
echo 'remove o4qs logs and corefiles'
rm -rvf /home/o4qs/workspace/log/*
rm -rvf /home/o4qs/workspace/dlog/*
rm -rvf /home/o4qs/workspace/core.*
echo 'remove o4ufc logs and corefiles'
rm -rvf /home/o4ufc/workspace/log/*
rm -rvf /home/o4ufc/workspace/dlog/*
rm -rvf /home/o4ufc/workspace/core.*
echo 'remove oracle logs' 
rm -rvf /home/oracle/product/11.2.0/dbhome_1/rdbms/log/*
echo 'finish clearning!!!!'
echo 'show current disk space...'
df -h
echo 'find the files of more than 50M...' 
find / -type f -size +50M | xargs du -h
4. crontab 文件格式
#1. 文件格式
[root@bat3963 fc_workspace]# 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

#2.使用实例
#实例1:每1分钟执行一次command
#* * * * * command

#实例2:每小时的第3和第15分钟执行
#3,15 * * * * command

#实例3:在上午8点到11点的第3和第15分钟执行
#3,15 8-11 * * * command

#实例4:每隔两天的上午8点到11点的第3和第15分钟执行
#3,15 8-11 */2 * * command

#实例5:每个星期一的上午8点到11点的第3和第15分钟执行
#3,15 8-11 * * 1 command

#实例6:每晚的21:30重启smb 
#30 21 * * * /etc/init.d/smb restart

#实例7:每月1、10、22日的4 : 45重启smb 
#45 4 1,10,22 * * /etc/init.d/smb restart

#实例8:每周六、周日的1 : 10重启smb
#10 1 * * 6,0 /etc/init.d/smb restart

#实例9:每天18 : 00至23 : 00之间每隔30分钟重启smb 
#0,30 18-23 * * * /etc/init.d/smb restart

#实例10:每星期六的晚上11 : 00 pm重启smb 
#0 23 * * 6 /etc/init.d/smb restart

#实例11:每一小时重启smb 
#* */1 * * * /etc/init.d/smb restart

#实例12:晚上11点到早上7点之间,每隔一小时重启smb 
#* 23-7/1 * * * /etc/init.d/smb restart

#实例13:每月的4号与每周一到周三的11点重启smb 
#0 11 4 * mon-wed /etc/init.d/smb restart

#实例14:一月一号的4点重启smb 
#0 4 1 jan * /etc/init.d/smb restart

#实例15:每小时执行/etc/cron.hourly目录内的脚本
#01 * * * * root run-parts /etc/cron.hourly
5. 使用问题汇总
5.1 每个定时任务会重复执行

​ 使用中发现每个定时任务到点会重复多次执行,经过排查发现是 crond 多开了几个进程,最后杀掉多余的进程即可恢复正常。

#1.查看进程
[root@bat3963 fc_workspace]# ps x | grep 'crond'
 1842 ?        Ss     0:41 crond
17000 ?        Ss     0:00 crond restart
17003 pts/3    S+     0:00 grep crond
#2.杀掉多余进程
[root@bat3963 fc_workspace]# kill -9 17000
#3.检查一下
[root@bat3963 fc_workspace]# ps x | grep 'crond'
 1842 ?        Ss     0:41 crond
17116 pts/3    S+     0:00 grep crond
5.2 修改任务内容

​ 经过测试发现,如果修改了任务脚本(例如上文提到的“/root/fc_workspace/test.sh”),下一次执行则会生效,不需要重启服务或者重新加载配置。

5.3.修改 crndtab 文件

​ 经过测试发现,如果修改了 crondtab 文件,则需要执行 /etc/init.d/crond reload 命令重新加载配置文件。

参考文档1:https://blog.csdn.net/mengzuchao/article/details/81172305?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2.control

​ 参考文档2:https://blog.csdn.net/qivan/article/details/53836426

道虽迩,不行不至;事虽小,不为不成。
原文地址:https://www.cnblogs.com/rock-cc/p/14664225.html