目的:所有产品统一启动脚本
功能:
1.可以根据不同参数进行程序启动、停止、重启等操作;
2.自动判断程序是否具有启动权限,是否为该程序的所属用户,如果不是,则提示错误,不允许启动。
脚本内容(start.sh):
#!/bin/bash
# start.sh
source ~/.bash_profile
PARM=$1
PORT=9010 #端口根据实际需要设置
#service_name需要根据实际jar包名做替换,不用考虑版本号,如:web-start,exchange-server,file-start等
service_name=evidence-start
tmp_dir="/home/dep/tmp" #需要根据实际情况修改这个临时目录,且需要手动创建该临时目录
user=`whoami`
owner=`ls -l | grep ${service_name} | grep "jar$" | awk '{print $3}'` #程序所属的用户名称
PID=`ps -ef | grep -w $PORT | grep -v grep | awk '{print $2}'`
#检查是否是程序所属用户,如果不是程序所属用户则不让执行该脚本
if [[ $owner == "" ]];then
echo -e "\033[31mERROR: The owner is empty and the [service_name] configuration may be wrong. \033[0m"
exit 1
elif [[ $user != $owner ]];then
echo -e "\033[31mERROR: The current user is $user, please use $owner user to execute.\033[0m"
exit 1
elif [[ $owner == "root" ]];then
echo -e "\033[31mERROR: The current user is root, root is not allowed to execute.\033[0m"
exit 1
fi
case $PARM in
'start')
if [[ -z $PID ]];then
echo "=================================================================="
echo "| Application starting at `date`. |"
echo "=================================================================="
java -Dserver.port=$PORT -Duser.timezone=Asia/Shanghai -Djava.io.tmpdir=${tmp_dir} -jar ${service_name}*.jar > stdout.log &
echo "| Application appears to still be running with PID $PID. |"
echo "=================================================================="
else
echo "=================================================================="
echo "| Application is running with PId $PID... |"
echo "=================================================================="
fi
;;
'stop')
if [[ $PID -ne 0 ]];then
kill -9 $PID
echo "=================================================================="
echo "| Application is shutting down successfully !!! |"
echo "=================================================================="
else
echo "=================================================================="
echo "| Nothing been done,Application had been shutted down! |"
echo "=================================================================="
fi
;;
'restart')
if [[ $PID -ne 0 ]];then
kill -9 $PID
echo "=================================================================="
echo "| Application having being down,please wait... |"
echo "=================================================================="
java -Dserver.port=$PORT -Duser.timezone=Asia/Shanghai -Djava.io.tmpdir=${tmp_dir} -jar ${service_name}*.jar & > stdout.log &
fi
if [[ $PID -eq 0 ]];then
echo "=================================================================="
echo "| Application had been shutted down.please wait staring... |"
echo "=================================================================="
java -Dserver.port=$PORT -Duser.timezone=Asia/Shanghai -Djava.io.tmpdir=${tmp_dir} -jar ${service_name}*.jar & > stdout.log &
fi
;;
'status')
if [[ -z ${pid} ]];then
echo "=================================================================="
echo "| Application is running with PId $PID... |"
echo "=================================================================="
else
echo "=================================================================="
echo "| Application is stoped !!! |"
echo "=================================================================="
fi
;;
esac