linux系统定时器CRON,linux系统的任务计划

时间:2022-11-28 23:36:11
设置系统定时器CRON


1日志是由业务系统产生的,我们可以设置web服务器每天产生一个新的目录,目录下面会产生多个日志文件,每个日志文件64M。
2设置系统定时器CRON,夜间在0点后,向HDFS导入昨天的日志文件。
3完成导入后,设置系统定时器,启动MapReduce程序,提取并计算统计指标。
4完成计算后,设置系统定时器,从HDFS导出统计指标数据到数据库,方便以后的即使查询。
0 8 * * * cd /root/hanfeng/shell; nohup sh termreg.sh & 
termreg.sh的内容是: 
#!/bin/sh  
set -x  
deal_date=${1:-`date --date '1 days ago' +%Y%m%d`}  
sh termreg2hbase.sh ${deal_date}  
sh termactivecount.sh ${deal_date}  
sh statTermReg.sh ${deal_date}  
statTermReg.sh
#!/bin/sh  
deal_date=${1:-`date --date '1 days ago' +%Y%m%d`}  
hadoop fs  -rmr /user/hdfs/result/stattermreg/${deal_date}/  
hadoop jar /root/hanfeng/lib/termreg.jar com.winksi.hadoop.regterminal.StatTermRegJob ${deal_date}  
cd /root/hanfeng/result/stattermreg  
rm -rf *  
hadoop fs -get /user/hdfs/result/stattermreg/${deal_date}/part*  
cat part* > result.txt  
echo "DELETE FROM term_reg wHERE report_date=${deal_date} ;" | mysql -h172.16.1.81 -P3308 -uadmin -ptonggangdasha reportdb  
echo "LOAD DATA local INFILE 'result.txt' INTO TABLE term_reg FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (report_date,channel_code,model,  
av,area_id,num) ;" | mysql -h172.16.1.81 -P3308 -uadmin -ptonggangdasha reportdb;  
这样生成10W条数据Load到mysql,只需要10S。


Linux crontab定时执行任务
基本格式 :
*  *  *  *  *  command
分 时 日 月 周 命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
crontab文件的一些例子:
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每晚的21:30重启apache。
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每月1、10、22日的4 : 45重启apache。
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每周六、周日的1 : 10重启apache。
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启apache。
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每星期六的11 : 00 pm重启apache。
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
每一小时重启apache
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
晚上11点到早上7点之间,每隔一小时重启apache
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
每月的4号与每周一到周三的11点重启apache
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart




【linux系统的任务计划】service crond status查看一下crond服务是否启动
关于cron任务计划功能的操作都是通过crontab这个命令来完成的。其中常用的选项有:
-u :指定某个用户,不加-u选项则为当前用户;
-e :制定计划任务;
-l :列出计划任务;
-r :删除计划任务。
前面部分为时间,后面部分要执行的命令
前面的时间是有讲究的,这个时间共分为5段,用空格隔开
第一段表示分钟(0-59),第二段表示小时(0-23),第三段表示日(1-31),第四段表示月(1-12),第五段表示周(0-7,0或者7都可以表示为周日)。
从左至右依次是:分,时,日,月,周(一定要牢记)!
crontab -e
1. 每天凌晨1点20分清除/var/log/slow.log这个文件;
1. 20 1 * * * echo "">/var/log/slow.log
2. 每周日凌晨3点执行’/bin/sh /usr/local/sbin/backup.sh’;
2. 0 03 * * 0 /bin/sh /usr/local/sbin/backup.sh
3. 每月14号4点10分执行’/bin/sh /usr/local/sbin/backup_month.sh’;
3. 10 04 14 * * /bin/sh /usr/local/sbin/backup_month.sh
4. 每隔8小时执行’ntpdate time.windows.com’;
4. 0 */8 * * * ntpdate time.windows.com
5. 每天的1点,12点,18点执行’/bin/sh /usr/local/sbin/test.sh’;
5. 0 1,12,18 * * /bin/sh /usr/local/sbin/test.sh
6. 每天的9点到18点执行’/bin/sh /usr/local/sbin/test2.sh’;
6. 0 9-18 * * * /bin/sh /usr/local/sbin/test2.sh