使用Shell制作一个服务启动脚本
经常使用脚本启动项目,于是乎整理了一个完整的共享给大伙!!
#!/bin/bash if [ -n "$2" ]; then echo $addr else addr="0.0.0.0:9000" fi; function status() { keyword=$1; pid=$(ps -ef | grep ${keyword} | awk 'NR==1{print $2}'); child=$(ps -ef | grep ${keyword} | awk 'NR==2{print $3}'); if [[ -n "$pid" ]] && [[ -n "$child" ]] && [[ "$pid" = "$child" ]]; then echo "the status of server:$keyword is started!"; return 1; else echo "the status of server:$keyword is stopped"; return 0; fi; } function start(){ keyword=$1; index=0 # 失败重启次数 times=10 until [ ! $index -lt $times ] do status $keyword; if [ "$?" = 0 ]; then echo "[server:$keyword] is starting..."; if [ "$keyword" = "celery" ]; then nohup python3 manage.py celery -A kw.celery_app:app worker -B -l info > celery.log 2>&1 & # python manage.py celery -A kw.celery_app:app worker -B -l info -D; else gunicorn -w 4 kw.wsgi:application -b ${addr} --reload -D; fi fi; status $keyword; if [ "$?" != 0 ]; then echo "[server:$keyword] is started."; break else sleep 10 index=`expr $index + 1` fi; done }; function stop(){ keyword=$1; status $keyword; if [ "$?" = 1 ]; then echo "[server:$keyword] is stopping..."; pid=$(ps -ef | grep ${keyword} | awk 'NR==1{print $2}') kill ${pid} echo "[server:$keyword] is stopped"; fi; }; action=$1 case $action in start) start $addr; start "celery"; ;; stop) stop $addr; stop "celery"; ;; restart) stop $addr; stop "celery"; start $addr; start "celery"; ;; status) status $addr; status "celery"; ;; *) echo "$0 {start|stop|restart|status}" exit 4 ;; esac
作者:符号哥
微信公众号:左侧为二维码
个人技术网站-编程符号网:http://www.itfh.cn
个人技术网站-IT源码网:http://www.itym.cn
新浪微博:https://weibo.com/u/2814576687
如果你想及时得到个人撰写文章以及著作的消息推送,或者想看看个人推荐的技术资料,可以扫描左边二维码(或者长按识别二维码)关注个人公众号。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

浙公网安备 33010602011771号