在系統維護中,編寫指令碼會幫助運維提高效率,現記錄一個通用的軟體啟動指令碼。指令碼內容如下:
#!/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