1、先看下最终实现效果
2、 安装 supervisor (web 界面监控)
yum install -y supervisor
systemctl start supervisord
systemctl enable supervisord
- 修改配置 /etc/supervisord.conf
cat <<EOF >> /etc/supervisord.conf
[inet_http_server]
port=*:9001
username=admin
password=cloudansys[supervisor]
EOF
- 添加配置文件 default.base 和 java.ini(空文件)
touch /etc/supervisord.d/java.ini
cat <<EOF >> /etc/supervisord.d/default.base
[program:lzgl-server] ; web 界面显示的服务名称
directory=/root/projects/lzgl-server ; 执行命令的目录
command=java -jar lzgl-server-1.0-SNAPSHOT.jar ; 执行的命令
priority=1 ; 数字越高,优先级越高
numprocs=1 ; 启动几个进程
autostart=true ; 随着supervisord的启动而启动
autorestart=true ; 自动重启
startretries=10 ; 启动失败时的最多重试次数
exitcodes=0 ; 正常退出代码
stopsignal=KILL ; 用来杀死进程的信号
stopwaitsecs=10 ; 发送SIGKILL前的等待时间
redirect_stderr=true ; 重定向stderr到stdout
;
;
;
EOF
systemctl restart supervisord
3、脚本 jar-operator.sh
,这个脚本需要依赖 supervisor
#!/bin/bash
:<<!
【脚本说明】
1、此脚本适用操作jar包服务;
2、建议把脚本放在 /usr/wlf 目录下;
3、哪个项目使用只需要在jar所在目录执行【ln -s /usr/wlf/jar-operator.sh operate.sh】;
4、在启动或重启时会判断是否配置了 web 监控,如果没配置则自动配置。
!
operate=$1
memsize=$2
homedir=$(cd $(dirname $0);pwd)
appfile=$(ls $homedir/*.jar)
app=${appfile##*/}
if [[ -z $memsize ]]; then
memsize=2048
fi
jarp="java -Xms${memsize}m -Xmx${memsize}m -jar "
function get_project(){
declare -a seplist
j=0
for i in `seq ${#app}`
do
char=${app:$i-1:1}
if [[ $char = '-' ]]; then
seplist[$j]=$i
j=$j+1
fi
done
if [[ $app =~ "SNAPSHOT" || $app =~ "RELEASE" ]]; then
size=${#seplist[*]}
index=$(expr $size - 1)
idx=$(expr ${seplist[$index]} - 1)
project=${app:0:${idx}}
echo $project
else
size=${#seplist[*]}
index=$(expr $size - 1)
idx=$(expr ${seplist[$index]} - 1)
project=${app:0:${idx}}
echo $project
fi
}
project=$(get_project)
pid=`ps -ef | egrep "$app" | egrep -v grep | awk '{print $2}'`
function custom_print(){
echo -e "\033[5;34m ***** \033[0m"
echo -e "\033[32m $@ ! \033[0m"
echo -e "\033[5;34m ***** \033[0m"
}
function file_has_str(){
if [[ ! $project ]]; then
echo "true"
else
grep -lrq $project]$ /etc/supervisord.d/java.ini && echo "true" || echo "false"
fi
}
msg='Please input the param 【<run [memsize]|kil|res [memsize]|sta|log>】'
function config_web(){
if [[ -d /etc/supervisord.d/ ]]; then
if [[ $(file_has_str) = "false" ]]; then
conf_def=/etc/supervisord.d/default.base
conf_use=/etc/supervisord.d/java.ini
title="[program:$project]"
dir="directory=$homedir"
cmd="command=$jarp $app"
sed -i "1c $title" $conf_def
sed -i "2c $dir" $conf_def
sed -i "3c $cmd" $conf_def
cat $conf_def >> $conf_use
/usr/bin/supervisorctl update
fi
fi
}
if [[ -z $operate || $operate = "help" || $operate = "-h" ]]; then
custom_print $msg
elif [[ $operate = "run" || $operate = "start" ]]; then
config_web
if [[ $pid ]]; then
msg='The service is already running'
custom_print $msg
else
if [[ -d /etc/supervisord.d/ ]]; then
/usr/bin/supervisorctl start $project > /dev/null
else
nohup $jarp $app > /dev/null 2>&1 &
fi
msg='Start success'
custom_print $msg
fi
elif [[ $operate = "restart" || $operate = "res" ]]; then
config_web
if [[ -d /etc/supervisord.d/ ]]; then
/usr/bin/supervisorctl restart $project > /dev/null
else
if [[ $pid ]]; then
kill -9 $pid
fi
nohup $jarp $app > /dev/null 2>&1 &
fi
msg='Restart success'
custom_print $msg
elif [[ $operate = "stop" || $operate = "kil" ]]; then
if [[ $pid ]]; then
if [[ -d /etc/supervisord.d/ ]]; then
/usr/bin/supervisorctl stop $project > /dev/null
else
kill -9 $pid
fi
msg='Stopped success'
custom_print $msg
else
msg='The service is already down'
custom_print $msg
fi
elif [[ $operate = "status" || $operate = "sta" ]]; then
if [[ $pid ]]; then
msg='RUNNING'
custom_print $msg
else
echo -e "\033[5;34m ***** \033[0m"
echo -e "\033[31m STOPPED ! \033[0m"
echo -e "\033[5;34m ***** \033[0m"
fi
elif [[ $operate = "log" ]]; then
logfile=$(ls $homedir/logs/*.log)
if [[ -e $logfile ]]; then
tail -f $logfile
else
msg='No log has been generated yet'
custom_print $msg
fi
else
msg='Please input the correct param 【<run [memsize]|kil|res [memsize]|sta|log>】'
custom_print $msg
fi
4、脚本使用方法
5、脚本jar-operator-without-supervisor.sh
,这个脚本无依赖
#!/bin/bash
:<<!
【脚本说明】
1、此脚本适用操作jar包服务;
2、建议把脚本放在 /usr/wlf 目录下;
3、哪个项目使用只需要在jar所在目录执行【ln -s /usr/wlf/jar-operator-without-supervisor.sh operate.sh】;
4、该脚本未配置web监控服务。
!
operate=$1
memsize=$2
homedir=$(cd $(dirname $0);pwd)
appfile=$(ls $homedir/*.jar)
app=${appfile##*/}
if [[ -z $memsize ]]; then
memsize=2048
fi
jarp="java -Xms${memsize}m -Xmx${memsize}m -jar "
function get_project(){
declare -a seplist
j=0
for i in `seq ${#app}`
do
char=${app:$i-1:1}
if [[ $char = '-' ]]; then
seplist[$j]=$i
j=$j+1
fi
done
if [[ $app =~ "SNAPSHOT" || $app =~ "RELEASE" ]]; then
size=${#seplist[*]}
index=$(expr $size - 1)
idx=$(expr ${seplist[$index]} - 1)
project=${app:0:${idx}}
echo $project
else
size=${#seplist[*]}
index=$(expr $size - 1)
idx=$(expr ${seplist[$index]} - 1)
project=${app:0:${idx}}
echo $project
fi
}
project=$(get_project)
pid=`ps -ef | egrep "$app" | egrep -v grep | awk '{print $2}'`
function custom_print(){
echo -e "\033[5;34m ***** \033[0m"
echo -e "\033[32m $@ ! \033[0m"
echo -e "\033[5;34m ***** \033[0m"
}
msg='Please input the param 【<run [memsize]|kil|res [memsize]|sta|log>】'
if [[ -z $operate || $operate = "help" || $operate = "-h" ]]; then
custom_print $msg
elif [[ $operate = "run" || $operate = "start" ]]; then
if [[ $pid ]]; then
msg='The service is already running'
custom_print $msg
else
nohup $jarp $app > /dev/null 2>&1 &
msg='Start success'
custom_print $msg
fi
elif [[ $operate = "restart" || $operate = "res" ]]; then
if [[ $pid ]]; then
kill -9 $pid
fi
nohup $jarp $app > /dev/null 2>&1 &
msg='Restart success'
custom_print $msg
elif [[ $operate = "stop" || $operate = "kil" ]]; then
if [[ $pid ]]; then
kill -9 $pid
msg='Stopped success'
custom_print $msg
else
msg='The service is already down'
custom_print $msg
fi
elif [[ $operate = "status" || $operate = "sta" ]]; then
if [[ $pid ]]; then
msg='RUNNING'
custom_print $msg
else
echo -e "\033[5;34m ***** \033[0m"
echo -e "\033[31m STOPPED ! \033[0m"
echo -e "\033[5;34m ***** \033[0m"
fi
elif [[ $operate = "log" ]]; then
logfile=$(ls $homedir/logs/*.log)
if [[ -e $logfile ]]; then
tail -f $logfile
else
msg='No log has been generated yet'
custom_print $msg
fi
else
msg='Please input the correct param 【<run [memsize]|kil|res [memsize]|sta|log>】'
custom_print $msg
fi
参考