Linux环境设置开机自启java应用(Linux定时任务)
crontab是Unix和Linux用于设置需要周期性被执行的指令,是Linux服务器很常用的技术,很多任务都会设置在crontab循环执行。
crontab : 提供给用户控制任务计划的命令,创建、删除、编辑任务计划等
详细过程:
1.在 CentOS上设置一个 crontab 的定时任务(每5分钟执行一次run_crontab.sh)
输入命令:
cd /etc
cat crontab(编辑用:vim crontab)
如图:
ps: 执行命令:sudo systemctl restart crond.service 使其立即生效
2.切到相应目录(我的是:cd /data/opt/script/shell)并编辑run_crontab.sh
命令:vim run_crontab.sh 添加自定义脚本,如图:
3.切换相应目录(我的是:cd /data/opt/script/)并编辑op_phmmt.sh,如图:
ps:op_phmmt.sh脚本,当应用意外挂了,会自动重启
4.验证脚本是否生效:
查看脚本运行状态:sh op_phmmt.sh status
运行脚本:sh op_phmmt.sh start
重启linux服务器:reboot -f
5.整个过程
补充:
1.run_crontab.sh脚本内容:
#!/bin/bash
. /etc/profile
LOG_HOME=/data/opt/monitorlog/crontab
DATE=$(date +"%F")
echo $(date +%Y-%m-%d%n%H:%M:%S) >> $(p=$LOG_HOME/$DATE;[[ ! -d "${p}" ]] && mkdir -p ${p};echo ${p}/$DATE.txt)
/data/opt/script/op_mariadb.sh monitor >> $LOG_HOME/$DATE/$DATE.txt
/data/opt/script/op_clickhouse.sh monitor >> $LOG_HOME/$DATE/$DATE.txt
/data/opt/script/op_docker.sh monitor >> $LOG_HOME/$DATE/$DATE.txt
/data/opt/script/op_redis.sh monitor >> $LOG_HOME/$DATE/$DATE.txt
/data/opt/script/op_kafka.sh monitor >> $LOG_HOME/$DATE/$DATE.txt
/data/opt/script/op_phmmt.sh monitor >> $LOG_HOME/$DATE/$DATE.txt
2.op_phmmt.sh脚本内容:
#!/bin/sh
# ./op_phmmt.sh start 启动 stop 停止 restart 重启 status 状态 monitor 监控(未启动->自动重启)
# JVM参数
JVM_OPTS="-Dname=$AppName -Duser.timezone=Asia/Shanghai -Xms256m -Xmx512m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC"
APP_HOME=/data/opt/phmmt
LOG_PATH=$APP_HOME/logs/$AppName.log
AppName2=phm-webapp
AppName=/data/opt/phmmt/phm-webapp-0.0.1-SNAPSHOT.jar
opUser=root
RE_LOG_HOME=/data/opt/monitorlog/crontab
RE_LOG_FILE=$RE_LOG_HOME/$(date +%F)/$(date +%F)_restart.txt
STATE_HOME=/data/opt/monitorlog/component-status
STATE_FILE=$STATE_HOME/$AppName2
if [ `whoami` != "$opUser" ];then
echo -e " only $opUser can run me"
exit 1
fi
if [ "$1" = "" ];
then
echo -e "\033[0;31m 未输入操作名 \033[0m \033[0;34m {start|stop|restart|status|monitor} \033[0m"
exit 1
fi
if [ "$AppName" = "" ];
then
echo -e "\033[0;31m 未输入应用名 \033[0m"
exit 1
fi
if [ ! -d "$RE_LOG_HOME/$(date +%F)/" ];
then
mkdir $RE_LOG_HOME/$(date +%F)
fi
function start()
{
PID=`ps -ef |grep java|grep $AppName2|grep -v grep|awk '{print $2}'`
if [ x"$PID" != x"" ]; then
echo "$AppName is running..."
else
nohup java $JVM_OPTS -Dspring.profiles.active=test -jar $AppName > /data/opt/phmmt/logs/phmmt.log 2>&1 &
# nohup java $JVM_OPTS -Dspring.profiles.active=prod -jar $AppName > /dev/null 2>&1 &
# nohup java $JVM_OPTS -Dspring.profiles.active=dev184 -jar $AppName 2>&1 | cronolog $APP_HOME/logs/log.%Y-%m-%d.out >> /dev/null &
echo "Start $AppName success..."
fi
}
function stop()
{
echo "Stop $AppName"
PID=""
query(){
PID=`ps -ef |grep java|grep $AppName2|grep -v grep|awk '{print $2}'`
}
query
if [ x"$PID" != x"" ]; then
kill -TERM $PID
echo "$AppName (pid:$PID) exiting..."
while [ x"$PID" != x"" ]
do
sleep 1
query
done
echo "$AppName exited."
else
echo "$AppName already stopped."
fi
}
function restart()
{
stop
sleep 2
start
}
function status()
{
PID=`ps -ef |grep java|grep $AppName2|grep -v grep|wc -l`
if [ $PID != 0 ];then
echo "$AppName is running..."
else
echo "$AppName is not running..."
fi
}
function monitor()
{
PID=`ps -ef |grep java|grep $AppName2|grep -v grep|wc -l`
if [ $PID != 0 ];then
echo "$AppName is running..."
echo $(date +%Y-%m-%d%n%H:%M:%S)_running > $STATE_FILE
else
start
echo "$AppName is not running,and restarting..."
# 重启保存到日志
echo $(date +%Y-%m-%d%n%H:%M:%S)_notrunning > $STATE_FILE
echo $(date +%Y-%m-%d%n%H:%M:%S) >> $RE_LOG_HOME/$(date +%F)/$(date +%F)_restart.txt
echo "$AppName is not running,and restarting..." >> $RE_LOG_HOME/$(date +%F)/$(date +%F)_restart.txt
fi
}
case $1 in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
status;;
monitor)
monitor;;
*)
esac