Linux下添加任务计划,计划内容以sh文件表示

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 打开的文件以下:php

0 * * * * /usr/sbin/ntpdate time.windows.com

*/10 * * * * (/usr/bin/php /home/monitor/hddusage.php)
0 */8 * * * (/usr/bin/php /home/monitor/hddhealth.php)

# 测试按期转存访问日志文件
1 0 * * * (/bin/bash /data02/xnou/binlog/rotate_nginx_log.sh >> /data02/xnou/binlog/task.log 2>&1 )




5、使设置生效
设置完成后,重启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
上面的例子表示每个月一、十、22日的4 : 45重启apache。


10 1 * * 6,0 /usr/local/apache/bin/apachectl restart
上面的例子表示每周6、周日的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

这命令能够打开计划任务文件,并在文件里添加任务html


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)java


0 * * * * /usr/sbin/ntpdate time.windows.com

*/10 * * * * (/usr/bin/php /home/monitor/hddusage.php)
0 */8 * * * (/usr/bin/php /home/monitor/hddhealth.php)

# remove expired files in '/tmp' directory.
0 1 * * * ( find /tmp/ -mtime +1 -exec rm -rf {} \; )

*/30 * * * * (/usr/bin/php /home/app/polyv/release-current/webapp/WEB-INF/sh/checkStatusQueueTask.php)
15 * * * * (/usr/bin/php /home/app/polyv/release-current/webapp/WEB-INF/sh/checkEncodeTask.php)
10 * * * * (/usr/bin/php /home/app/polyv/release-current/webapp/WEB-INF/sh/checksms.php)

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)

30 8 * * * (wget http://misc.polyv.net:8080/send_expiration_email)

59 5 * * * (/usr/bin/php /home/app/polyv/release-current/webapp/WEB-INF/sh/kw_flow_date.php)
22,42,58 * * * * (/usr/bin/php /home/app/polyv/release-current/webapp/WEB-INF/sh/kw_flow.php)

# 删除最后修改时间为1天前的源视频文件
30 1 * * * ( find /data/htmlfile/?/video_source -mtime +1 -type f -exec rm -f {} \; )
# 硬盘监控
#*/10 * * * * /usr/bin/php /data/syssh/alert.php
*/10 * * * * /data/syssh/killffmpeg.sh
* * * * * (find /tmp/ -name "*.flv" -mmin +600 -exec rm -rf {} \;)
* * * * * (find /tmp/ -name "*.mp4" -mmin +600 -exec rm -rf {} \;)
* * * * * (find /data/htmlfile/?/video_target -mmin +200 -type f -exec rm -f {} \;)
* * * * * (find /data/htmlfile/?/video_source -mmin +200 -type f -exec rm -f {} \;)
#10 * * * * (>/data/mysql/data/log_error.err)

04 18 * * * (/bin/bash /data/nginx/sbin/cut-log-school.sh)


# 测试按期转存访问日志文件
1 0 * * * (/bin/bash /data02/xnou/binlog/rotate_nginx_log.sh >> /data02/xnou/binlog/task.log 2>&1 )

*/5 * * * * ( find /data/htmlfile/video_image/ -maxdepth 2 -type d -user root -exec chown -R web:web {} \; )
*/5 * * * * ( find /data/htmlfile/*/video_source/ -maxdepth 1 -type d -user root -exec chown -R web:web {} \; )
*/5 * * * * (/bin/sh /home/app/polyv/release-current/webapp/WEB-INF/sh/runjava.sh "com.ovp.test.TestAllVideoJson")

# 无限极首页静态化
*/5 * * * * ( wget "http://v.infinitus.com.cn/wxj/home" -O "/home/workspace/wuxianji/target/wuxianji/index.html" 2>&1 )
  参数很简单,这几个单词相信你们一看就明白了,格式跟方法1讲的同样,须要注意的是 crontab -e 写的是用户本身的计划任务,文件存放在如下目录