crontab定时清理日志

1.创建shell脚本

vi test_cron.sh

#!/bin/bash
#echo "====`date`====" >> /game/webapp/test_cron.log
find /home/logs/* -mtime +7 -name "*.log.*" -exec rm -rf {} \;
find /game/webapp/tomcats/parwa/logs/*  -mtime +7 -name "*.log*" -exec rm -rf {} \;
find /game/webapp/tomcats/comm/logs/*  -mtime +7 -name "*.log" -exec rm -rf {} \;
find /game/webapp/tomcats/api/logs/*  -mtime +7 -name "*.log" -exec rm -rf {} \;
清理7天前的日志文件
保存文件

给脚本赋予权限

chmod 755 test_cron.sh

2.编写定时任务

crontab -e

10 0 * * * /game/webapp/test_cron.sh
####每天凌晨定时跑一次
按ctrl+X ,再按Y保存

到这里定时任务已经配置成功了。

3.查看cron脚本是否启动

cat /var/log/syslog   

Jul 26 00:08:21 parwa crontab[32352]: (root) LIST (root)
Jul 26 00:10:01 parwa CRON[32366]: (root) CMD (/game/webapp/test_cron.sh)
Jul 26 00:15:01 parwa CRON[32463]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Jul 26 00:17:01 parwa CRON[32486]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)

posted @ 2019-06-24 16:41  taotao12  阅读(2889)  评论(0编辑  收藏  举报