代码重启脚本

import os

def daima():
    # 进入后端目录/opt/chinavasionapp
    if not os.path.exists('/opt/chinavasionapp'):
        print('/opt/chinavasionapp目录不存在')

    os.chdir('/opt/chinavasionapp')
    # 从git下更新代码
    os.system('git stash;git pull')
    # 后端代码迁移
    print('代码迁移')
    os.chdir('/opt/chinavasionapp/Pinshop')
    os.system('pip3 install -r requirements.txt')
    #os.system('rm -rf /opt/chinavasionapp/Pinshop/migrations')
    #os.system('python3 manage.py db init')
    #os.system('python3 manage.py db migrate')
    #os.system('python3 manage.py db upgrade')
    print('代码迁移完成')
    # 进入前端目录/opt/dist
    if not os.path.exists('/opt/dist'):
        print('/opt/dist目录不存在')
    #os.chdir('/opt/dist')
    #os.system('git pull')
    #print('前端代码迁移完成')
    #os.system('nginx -s reload')
    print('nginx进程重启')
    # 杀掉gunicorn进程
    # os.system('kill -s 9 `pgrep gunicorn`')
    # os.system('ps aux | grep "gunicorn" |grep -v grep| cut -c 9-15 | xargs kill ')
    os.system('ps aux | grep 7001 |grep -v grep| cut -c 9-15 | xargs kill ')
    print('杀掉gunicorn进程')
    # 重启gunicorn进程
    os.chdir('/opt/chinavasionapp/Pinshop')
    os.system('gunicorn  manage:app  -c ./guni_conf.py -b 0.0.0.0:7001  --chdir . 1>/dev/null 2>&1 &')
   # os.system('gunicorn  manage:app  -c ./guni_conf.py --chdir . 1>/dev/null 2>&1 &')
    os.system('exit')
    print('重启服务成功')
    return
if __name__=='__main__':
    daima()

白俊遥博客

 ps aux | grep 7000 |grep -v grep| cut -c 10-15

说明:

grep 7000的输出结果是,所有含有关键字“7000”的进程。

grep -v grep是在列出的进程中去除含有关键字“grep”的进程。

cut -c 9-15是截取输入行的第10个字符到第15个字符,而这正好是进程号PID。

xargs kill -s 9中的xargs命令是用来把前面命令的输出结果(PID)作为“kill -s 9”命令的参数,并执行该命令。“kill -s 9”会强行杀掉指定进程。



打赏,支持一下

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

Powered by WEIXIA.XIN,学的不仅是技术,更是梦想!!!

Davidvivi博客
请先登录后发表评论
  • 最新评论
  • 总共 0条评论