在系统维护中,编写脚本会帮助运维提高效率,现记录一个通用的软件启动脚本。脚本内容如下:
#!/bin/bash
# 软件启动程序包名称
APP_NAME=datadog-4.2.0.jar
# 软件名称
APP_NAME2=Datadog
usage() {
echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
exit 1
}
is_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
# 启动
start(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME2} is already running. pid=${pid} ."
else
nohup jre/bin/java -jar $APP_NAME >> run.log 2>&1 &
fi
}
# 停止
stop(){
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME2} is not running"
fi
}
# 查看软件运行状态
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME2} is running. pid is ${pid}"
else
echo "${APP_NAME2} is not running."
fi
}
# 重启,先停止再启动
restart(){
stop
start
}
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac