crontab 表达式说明
服务状态查看
Crontab 默认使用 vi编辑器
//开始编辑一个周期定时任务
[root@master ~]# crontab -e
直接输入命令后保存
*/1 * * * * date >>/root/out.log
//参看周期定时任务
[root@master ~]# crontab -l
*/1 * * * * date >>/root/out.log
[root@master ~]# ll
总用量 20
-rw-------. 1 root root 1446 2月 12 21:32 anaconda-ks.cfg
drwxr-xr-x. 2 root root 174 2月 15 18:10 download
-rw-r--r--. 1 root root 589 2月 13 15:40 id_dsa_1024.pub
-rw-r--r--. 1 root root 43 2月 16 13:57 out.log
drwxrwxrwx. 2 root root 134 2月 16 10:57 test
-rw-r--r--. 1 root root 7417 2月 16 09:34 ysq
[root@master ~]# tail -f out.log
2017年02月 16日 星期四 13:57:02 CST
2017年02月 16日 星期四 13:58:01 CST
2017年02月 16日 星期四 13:59:01 CST
//停止执行在 命令前加上 #注释符就好
[root@master ~]# crontab -l
#*/1 * * * * date >>/root/out.log
//删除周期执行任务
[root@master ~]# crontab -r
//以文件的方式配置周期 定时计划
[root@master ~]# cd /etc/cron.d
[root@master cron.d]# ll
总用量 4
-rw-r--r--. 1 root root 128 3月 31 2016 0hourly
[root@master cron.d]#
[root@master cron.d]# ll
总用量 4
-rw-r--r--. 1 root root 128 3月 31 2016 0hourly
//复制该文件 修改 编辑一下文件 写入要定时计划命令 将文件保存到该目录下
[root@master cron.d]# vim 0hourly
文件格式
查看任务日志
系统已有的周期计划
service crond restart
查看执行日志 :
tail -f /var/log/cron
定时任务
30 23 * * * /opt/resend_restart.sh
resend_restart.sh
-------------------------------------------------------------------------------------------------------------------------------
#!/bin/bash
export JAVA_HOME=/opt/jdk1.8.0_171
export MYSQL_HOME=/opt/mysql-5.7.22-linux-glibc2.12-x86_64
export CLASSPATH=.:$JAVA_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/lib:$MYSQL_HOME/bin
date >> /opt/resend_restart.log
echo '***********************resend restart**************************' >> /opt/resend_restart.log
echo '--------------------------one restart-------------------------' >> /opt/resend_restart.log
/opt/webapps/bstation/btransport1/bin/shutdown.sh >> /opt/resend_restart.log
/opt/webapps/bstation/btransport1/bin/startup.sh >> /opt/resend_restart.log
echo '--------------------------two restart-------------------------' >> /opt/resend_restart.log
/opt/webapps/bstation/btransport2/bin/shutdown.sh >>/opt/resend_restart.log
/opt/webapps/bstation/btransport2/bin/startup.sh >>/opt/resend_restart.log
echo '*************************start end******************************' >> /opt/resend_restart.log
echo -e '\n' >> /opt/resend_restart.log
-------------------------------------------------------------------------------------------------------------------------------
遇到的坑 :
脚本执行的时候,环境变量找不到,脚本执行有些问题
解决方法 :
在脚本种加入 环境变量
export JAVA_HOME=/opt/jdk1.8.0_171
export MYSQL_HOME=/opt/mysql-5.7.22-linux-glibc2.12-x86_64
export CLASSPATH=.:$JAVA_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/lib:$MYSQL_HOME/bin