编写shell脚本,用于启动、关闭jar程序:
#!/bin/bash
#description: 启动重启server服务
#需要配置环境变量后才能直接使用java这些变量
export JAVA_HOME=/data/jdk1.8.0_111
export PATH=$PATH:$JAVA_HOME/bin
#启动命令所在目录
HOME='/data/dingtalk'
#过滤查询执行.jar的线程PID
pid=`ps -ef|grep dingtalk.jar|grep -v grep|awk '{printf $2}'`
#执行jar
start(){
if [ -n "$pid" ]; then
echo "server already start,pid:$pid"
return 0
fi
#进入命令所在目录
cd $HOME
nohup java -jar $HOME/dingtalk.jar > $HOME/server.log 2>&1 & #启动聊天服务器 把日志输出到HOME目录的server.log文件中
echo "start at port:$pid"
}
#停止
stop(){
if [ -z "$pid" ]; then
echo "not find program on pid:$pid"
return 0
fi
#结束程序,使用讯号2,如果不行可以尝试讯号9强制结束
kill -9 $pid
rm -rf $pid
echo "kill program use signal 2,pid:$pid"
}
status(){
if [ -z "$pid" ]; then
echo "not find program on pid:$pid"
else
echo "program is running,pid:$pid"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
$0 stop
sleep 2
$0 start
;;
status)
status
;;
*)
echo "Usage: {start|stop|status}"
;;
esac
exit 0
定时执行:
crontab -e
#配置定时执行与停止
50 10 * * * bash -x /data/dingtalk/dingtalk.sh start
52 10 * * * bash -x /data/dingtalk/dingtalk.sh stop
crontab相关命令:
/sbin/service crond start //启动服务
/sbin/service crond stop //关闭服务
/sbin/service crond restart //重启服务
/sbin/service crond reload //重新载入配置