您的位置:首页 > 运维架构 > Linux

linux下为程序创建启动和关闭的的sh文件,scrapyd为例

2012-12-03 20:40 363 查看
对于一些未提供service管理的程序 每次启动和关闭都要加上全部路径,想到可以做一个简单的启动和关闭控制的文件

下面以scrapy启动server为例,文件名为run.sh:

Python代码


#端口号,根据此端口号确定PID

PORT=6800

#启动命令所在目录

HOME='/home/jmscra/scrapy/'

#查询出监听了PORT端口TCP协议的程序

pid=`netstat -lnopt | grep :$PORT | awk '/python/{gsub(/\/python/,"",$7);print $7;}'`

start(){

if [ -n "$pid" ]; then

echo "server already start,pid:$pid"

return 0

fi

#进入命令所在目录

cd $HOME

nohup scrapy server & #启动scrpayd服务器 把日志输出到HOME目录的nohup.out文件中

echo "start at port:$PORT"

}

stop(){

if [ -z "$pid" ]; then

echo "not find program on port:$PORT"

return 0

fi

#结束程序,使用讯号2,如果不行可以尝试讯号9强制结束

kill -2 $pid

echo "kill program use signal 2,pid:$pid"

}

status(){

if [ -z "$pid" ]; then

echo "not find program on port:$PORT"

else

echo "program is running,pid:$pid"

fi

}

case $1 in

start)

start

;;

stop)

stop

;;

status)

status

;;

*)

echo "Usage: {start|stop|status}"

;;

esac

exit 0

使用时则可以

/path/run.sh start启动

/path/run.sh stop停止

/path/run.sh status 查看运行程序的pid

不要忘记为run.sh添加可执行权限
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: