每次启动程序都要敲一堆命令,终止程序都要ps+grep找到程序pid然后kill,太麻烦了!
花了点时间写了个程序启动停止脚本,如下:
#! /bin/bash
readonly CMD=$1
readonly PARAM=$2
readonly BIN="./your_bin"
start() {
local bin=$1
local param=$2
if [ -f .pid ];then
echo "${bin} is running"
return 0;
fi
if [ ${param}"" == "-d" ];then
nohup ${bin} &
echo $! > .pid
echo "${bin} started"
return 0
fi
${bin}
}
stop() {
pid=`cat .pid`
if [ $pid"" = "" ]; then
echo "already stopped"
exit
fi
kill -2 $pid
rm .pid
echo "stopped"
}
help() {
echo "sh start.sh [start|stop|restart] [-d]"
}
case "$CMD" in
start)
start $BIN $PARAM
;;
stop)
stop
;;
restart)
stop
sleep 1
start
;;
*)
help
;;
esac
只需要配置一个BIN变量即可实现程序的启动和停止,十分简单。
赞赏微信赞赏
支付宝赞赏