需求
由于使用Canal进行增量同步数据(Mysql->ES),Canal里面包含deployer和adapter这两个服务,服务器性能不佳,ES总是宕机,ES设置的有自启操作,但是ES重启之后,deployer和adapter这两个脚本的服务就起不来,没有设置自启操作。故:设置定时任务执行该脚本
Crontab
service crond start #启动服务
service crond stop #关闭服务
service crond restart #重启服务
service crond reload #重新载入配置
service crond status #查看启动状态
crontab -e 进行编辑操作
敲重点
*/1 * * * * . /etc/profile;/bin/sh /home/admin/Desktop/yzx/canal/ada_task.sh
注:
(1)语 法:
crontab [-u <用户名称>][配置文件] 或 crontab { -l | -r | -e }
-u #<用户名称> 是指设定指定<用户名称>的定时任务,这个前提是你必须要有其权限(比如说是 root)才能够指定他人的时程表。如果不使用 -u user 的话,就是表示设定自己的定时任务。
-l #列出该用户的定时任务设置。
-r #删除该用户的定时任务设置。
-e #编辑该用户的定时任务设置。
(2)命令时间格式 :
* * * * * command
分 时 日 月 周 命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
(3)一些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
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart #晚上11点到早上7点之间,每隔一小时重启apache
* */1 * * * /usr/local/etc/rc.d/lighttpd restart #每一小时重启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 #一月一号的4点重启apache
*/30 * * * * /usr/sbin/ntpdate cn.pool.ntp.org #每半小时同步一下时间
0 */2 * * * /sbin/service httpd restart #每两个小时重启一次apache
50 7 * * * /sbin/service sshd start #每天7:50开启ssh服务
50 22 * * * /sbin/service sshd stop #每天22:50关闭ssh服务
0 0 1,15 * * fsck /home #每月1号和15号检查/home 磁盘
1 * * * * /home/bruce/backup #每小时的第一分执行 /home/bruce/backup这个文件
00 03 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \; #每周一至周五3点钟,在目录/home中,查找文件名为*.xxx的文件,并删除4天前的文件。
30 6 */10 * * ls #每月的1、11、21、31日是的6:30执行一次ls命令
crontab -l 查看列表
crontab -r 删除操作
本人不用
后续
编辑好定时任务之后
重启 service crond restart
tail -f /var/log/cron 实时查看定时任务执行情况
ctrl+c 退出
超后话
deployer服务脚本:
dep_task.sh
#!/bin/sh
. /etc/profile #很重要
. ~/.bash_profile #很重要
deployer=`netstat -an | grep ":11112" | awk '$1 == "tcp" && $NF == "LISTEN" {print $0}' | wc -l`
if [ $deployer -eq 0 ];then
#如果端口没有占用的话启动服务
sh /home/admin/Desktop/yzx/canal/deployer/bin/restart.sh # 全路径 配权限 777
fi
chmod 777 dep_task.sh 赋权
adapter服务脚本
ada_task.sh
#!/bin/sh
. /etc/profile #很重要
. ~/.bash_profile #很重要
adapter=`netstat -an | grep ":8081" | awk '$1 == "tcp" && $NF == "LISTEN" {print $0}' | wc -l`
if [ $adapter-eq 0 ];then
#如果端口没有占用的话启动服务
sh /home/admin/Desktop/yzx/canal/adapter/bin/restart.sh # 全路径 配权限 777
fi
chmod 777 ada_task.sh 赋权