Linux下添加任务计划

sh文件内容:
#/bin/sh
DATE=`date +%Y%m%d`
cd /your/log/path/
for f in *; do
cp $f  /path/to/save/$DATE_$f;
done


记得要给这个文件加可执行权限: chmod a+x   xxx.sh


for f 中的f 是会换成当前文件的。


#/bin/sh
cd /your/log/path/
for f in *; do
  cp $f  /path/to/save/`date +%Y%m%d`_$f;
done
#这句会删除当前目录中10*24小时之前创建的文件,就是10天前的。
find . -ctime +10 -exec rm -f {} \;
 要注意`date +%Y%m%d` 两边是反引号,就是键盘TAB上面1前面那个键。不要打错。






加入到计划任务是crontab -e  
然后就像使用vi一样加入一行 (每天的2点01分执行)
01 02 * * * /path/to/your/xxx.sh
保存退出。然后如果是RH系统最好重启一下crond 服务,就OK了。

命令crontab -e 打开的文件如下:

[html]  view plain  copy
  1. 0 * * * * /usr/sbin/ntpdate time.windows.com  
  2.   
  3. */10 * * * * (/usr/bin/php /home/monitor/hddusage.php)  
  4. 0 */8 * * * (/usr/bin/php /home/monitor/hddhealth.php)  
  5.   
  6. # 测试定期转存访问日志文件  
  7. 1 0 * * * (/bin/bash /data02/xnou/binlog/rotate_nginx_log.sh >> /data02/xnou/binlog/task.log 2>&1 )  




五、使设置生效
设置完成后,重启cron即可使设置的计划任务定时执行了,重启命令如下:
有些linux系统是service cron restart
crond服务的重启命令是
service cron stop
service cron start吧






重启命令:service crond restart
查看任务状态命令
[root@ovp-l5520-216 binlog]# service crond status
crond (pid  1332) 正在运行...






在运维行业里流行着这么一句话:能自动完成的,绝不手工去操作;这就涉及到Linux下的计划任务crond 的设置,下面介绍crond 是如何设置的。


方法1:编辑/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
# | | | | |
# * * * * * command to be executed


格式说明:


第1列 分钟1~59
第2列 小时1~23(0表示子夜)
第3列 日1~31
第4列 月1~12
第5列 星期0~6(0表示星期天)
第6列 要运行的命令


 


例子:


01 * * * * ntpdate time.windows.com


上面的例子表示每小时01分同步一次系统时间。


 


59 23 * * * /etc/init.d/httpd restart
上面的例子表示每晚的23:59重启apache。


45 4 1 * * /etc/init.d/httpd restart


上面的例子表示每月1日的4:45分重启apache。




45 4 1,10,22 * * /etc/init.d/httpd restart
上面的例子表示每月1、10、22日的4 : 45重启apache。


10 1 * * 6,0 /usr/local/apache/bin/apachectl restart
上面的例子表示每周六、周日的1 : 10重启apache。


0,30 18-23 * * * /usr/local/apache/bin/apachectl restart
上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启apache。


0 23 * * 6 /usr/local/apache/bin/apachectl restart
上面的例子表示每星期六的晚上11 : 00 pm重启apache。


* */1 * * * /usr/local/apache/bin/apachectl restart
每一小时重启apache


* 23-7/1 * * * /usr/local/apache/bin/apachectl restart
晚上11点到早上7点之间,每隔一小时重启apache


0 11 4 * mon-wed /usr/local/apache/bin/apachectl restart
每月的4号与每周一到周三的11点重启apache


0 4 1 jan * /etc/init.d/httpd restart
一月一号的4点重启apache


 


方法2:crontab -e

这命令可以打开计划任务文件,并在文件里添加任务


usage: crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]
(default operation is replace, per 1003.2)
-e (edit user's crontab)
-l (list user's crontab)
-r (delete user's crontab)
-i (prompt before deleting user's crontab)

-s (selinux context)


[html]  view plain  copy
  1. 0 * * * * /usr/sbin/ntpdate time.windows.com  
  2.   
  3. */10 * * * * (/usr/bin/php /home/monitor/hddusage.php)  
  4. 0 */8 * * * (/usr/bin/php /home/monitor/hddhealth.php)  
  5.   
  6. # remove expired files in '/tmp' directory.  
  7. 0 1 * * * ( find /tmp/ -mtime +1 -exec rm -rf {} \; )  
  8.   
  9. */30 * * * * (/usr/bin/php /home/app/polyv/release-current/webapp/WEB-INF/sh/checkStatusQueueTask.php)  
  10. 15 * * * * (/usr/bin/php /home/app/polyv/release-current/webapp/WEB-INF/sh/checkEncodeTask.php)  
  11. 10 * * * * (/usr/bin/php /home/app/polyv/release-current/webapp/WEB-INF/sh/checksms.php)  
  12.   
  13. 30,50 * * * * (/bin/sh /home/app/polyv/release-current/webapp/WEB-INF/sh/runjava.sh com.cc.ovp.web.stat.MRTask >> /dev/null 2>&1)  
  14.   
  15. 30 8 * * * (wget http://misc.polyv.net:8080/send_expiration_email)  
  16.   
  17. 59 5 * * * (/usr/bin/php /home/app/polyv/release-current/webapp/WEB-INF/sh/kw_flow_date.php)  
  18. 22,42,58 * * * * (/usr/bin/php /home/app/polyv/release-current/webapp/WEB-INF/sh/kw_flow.php)  
  19.   
  20. # 删除最后修改时间为1天前的源视频文件  
  21. 30 1 * * * ( find /data/htmlfile/?/video_source -mtime +1 -type f -exec rm -f {} \; )  
  22. # 硬盘监控  
  23. #*/10 * * * * /usr/bin/php /data/syssh/alert.php  
  24. */10 * * * * /data/syssh/killffmpeg.sh  
  25. * * * * * (find /tmp/ -name "*.flv" -mmin +600 -exec rm -rf {} \;)  
  26. * * * * * (find /tmp/ -name "*.mp4" -mmin +600 -exec rm -rf {} \;)  
  27. * * * * * (find /data/htmlfile/?/video_target -mmin +200 -type f -exec rm -f {} \;)  
  28. * * * * * (find /data/htmlfile/?/video_source -mmin +200 -type f -exec rm -f {} \;)  
  29. #10 * * * * (>/data/mysql/data/log_error.err)  
  30.   
  31. 04 18 * * * (/bin/bash /data/nginx/sbin/cut-log-school.sh)  
  32.   
  33.   
  34. # 测试定期转存访问日志文件  
  35. 1 0 * * * (/bin/bash /data02/xnou/binlog/rotate_nginx_log.sh >> /data02/xnou/binlog/task.log 2>&1 )  
  36.   
  37. */5 * * * * ( find /data/htmlfile/video_image/ -maxdepth 2 -type d -user root -exec chown -R web:web {} \; )  
  38. */5 * * * * ( find /data/htmlfile/*/video_source/ -maxdepth 1 -type d -user root -exec chown -R web:web {} \; )  
  39. */5 * * * * (/bin/sh /home/app/polyv/release-current/webapp/WEB-INF/sh/runjava.sh "com.ovp.test.TestAllVideoJson")  
  40.   
  41. # 无限极首页静态化  
  42. */5 * * * * ( wget "http://v.infinitus.com.cn/wxj/home" -O "/home/workspace/wuxianji/target/wuxianji/index.html" 2>&1 )  




 


参数很简单,这几个单词相信大家一看就明白了,格式跟方法1讲的一样,需要注意的是 crontab -e 写的是用户自己的计划任务,文件存放在以下目录
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值