(摘自百度百科)Supervisor是用Python開發的一套通用的進程管理程序,能將一個普通的命令行進程變為后台daemon,並監控進程狀態,異常退出時能自動重啟。它是通過fork/exec的方式把這些被管理的進程當作supervisor的子進程來啟動,這樣只要在supervisor的配置文件中,把要管理的進程的可執行文件的路徑寫進去即可。也實現當子進程掛掉的時候,父進程可以准確獲取子進程掛掉的信息的,可以選擇是否自己啟動和報警。supervisor還提供了一個功能,可以為supervisord或者每個子進程,設置一個非root的user,這個user就可以管理它對應的進程。
Supervisor安裝
'''
環境:Centos7
安裝wget:yum install wget
下載Supervisor源碼包
'''
# [root@localhost software]# yum install wget
# 已加載插件:fastestmirror
# Loading mirror speeds from cached hostfile
# * base: mirror.lzu.edu.cn
# * extras: mirror.lzu.edu.cn
# * updates: mirrors.163.com
# 正在解決依賴關系
# --> 正在檢查事務
# ---> 軟件包 wget.x86_64.0.1.14-18.el7_6.1 將被 安裝
# [root@localhost software]# wget https://files.pythonhosted.org/packages/d3/7f/c780b7471ba0ff4548967a9f7a8b0bfce222c3a496c3dfad0164172222b0/supervisor-4.2.2.tar.gz
# --2021-09-24 15:45:28-- https://files.pythonhosted.org/packages/d3/7f/c780b7471ba0ff4548967a9f7a8b0bfce222c3a496c3dfad0164172222b0/supervisor-4.2.2.tar.gz
# 正在解析主機 files.pythonhosted.org (files.pythonhosted.org)... 151.101.77.63, 2a04:4e42:12::319
# 正在連接 files.pythonhosted.org (files.pythonhosted.org)|151.101.77.63|:443... 已連接。
# 已發出 HTTP 請求,正在等待回應... 200 OK
# 長度:463657 (453K) [application/x-tar]
# 正在保存至: “supervisor-4.2.2.tar.gz”
Supervisor配置
'''
1、解壓源碼包
tar -zxvf supervisor-4.2.2.tar.gz
'''
# root@localhost software]# tar -zxvf supervisor-4.2.2.tar.gz
# supervisor-4.2.2/
# supervisor-4.2.2/CHANGES.rst
# supervisor-4.2.2/COPYRIGHT.txt
'''
2、安裝python支持
yum install python-setuptools
'''
# [root@localhost supervisor-4.2.2]# yum install python-setuptools
# 已加載插件:fastestmirror
# Loading mirror speeds from cached hostfile
'''
3、編譯源碼包
python setup.py install
'''
# [root@localhost supervisor-4.2.2]# python setup.py install
# running install
# running bdist_egg
# running egg_info
# writing requirements to supervisor.egg-info/requires.txt
'''
4、編譯后刪除其他多余文件、除了build、dist兩個文件夾其他都是多余的
'''
# [root@localhost supervisor-4.2.2]# ll
# 總用量 0
# drwxr-xr-x. 4 root root 43 9月 24 15:51 build
# drwxr-xr-x. 2 root root 40 9月 24 15:51 dist
'''
5、創建配置文件、編輯配置文件、賦予文件權限
'''
# echo_supervisord_conf > /usr/etc/supervisord.conf
# [root@localhost supervisor-4.2.2]# vi /usr/etc/supervisord.conf
# chmod 777 /usr/etc/supervisord.conf
# [root@localhost supervisor-4.2.2]# mkdir config
# /usr/etc/supervisord.conf文件主要配置兩個關鍵項
# [supervisord]
# user=普通用戶名稱 ; setuid to this UNIX account at startup; recommended if root
# [include]
# files = /software/supervisor-4.2.2/config # 這個是以后的項目路徑
'''
6、查看版本
'''
# [root@localhost supervisor-4.2.2]# supervisord -v
# 4.2.2
'''
7、解決python2.7.sock報錯的問題
'''
# [root@localhost supervisor-4.2.2]# /usr/bin/python2 /usr/bin/supervisord -c /usr/etc/supervisord.conf
'''
8、更新配置
'''
# supervisorctl update
'''
9、配置一個程序項目配置
注意:這里使用hello_world只是作為一個說明,一般項目指的都是一直運行的進程服務,比如:redis、tomcat、nginx等等。
'''
# [root@localhost config]# vi hello_world.config
# [program:hello_world]
# command=/usr/bin/python2 /software/supervisor-4.2.2/hello_world.py
# priority=998
# autostart=true
# autorestart=true
# startsecs=60
# startretries=3
# stopsignal=TERM
# stopwaitsecs=10
# user=root
# stdout_logfile=/software/supervisor-4.2.2/logs/hello_world.log
# stdout_logfile_maxbytes=100MB
# stdout_logfile_backups=10
# stdout_capture_maxbytes=1MB
# stderr_logfile=/software/supervisor-4.2.2/logs/hello_world.log
# stderr_logfile_maxbytes=100MB
# stderr_logfile_backups=10
# stderr_capture_maxbytes=1MB
'''
10、創建hello_world項目
注意:這里使用hello_world只是作為一個說明,一般項目指的都是一直運行的進程服務,比如:redis、tomcat、nginx等等。
'''
# [root@localhost supervisor-4.2.2]# vi hello_world.py
# # -*- coding:utf-8 -*-
# print "我是hello_world程序"
'''
11、重啟配置的所有程序
'''
# [root@localhost supervisor-4.2.2]# supervisorctl reload
# Restarted supervisord
'''
12、啟動或停止某個項目
注意:這里使用hello_world只是作為一個說明,一般項目指的都是一直運行的進程服務,比如:redis、tomcat、nginx等等。
'''
# supervisorctl start 項目名稱
# supervisorctl start hello_world
# supervisorctl stop 項目名稱
# supervisorctl stop hello_world
【往期精選】
● 如何將一個python應用以docker鏡像的方式來運行?
● python-celery專注於實現分布式異步任務處理、任務調度的插件!
● python遠程服務操作工具:fabric,遠程命令、本地命令、服務器操作利器!
● python超贊插件you-get,執行一行命令即可下載、命令行下載工具推薦!
● 辦公自動化:Python-win32com自動將word文檔轉換成pdf格式!
● pandas數據統計插件的連接函數concat()妙用,靈活處理數據對象!
● Git LFS 3.0.0 發布,對大文件進行版本控制的 Git 擴展
● python有序序列的字典序列推導式運用技巧!
● Django 4.0 alpha 1 發布
● python經典有序序列的list列表推導式實踐運用
● python常用轉義字符串總結:各種字符轉義的不同、如何取消轉義字符效果?
● python內置函數通過字符串的方式來執行函數代碼塊,類似java的反射機制相當強大!
● 磨刀不誤砍柴工,PyCharm開發工具的常規配置,充分提高開發效率!
● python-openpyxl Excel的單元格樣式設置,包括字體、樣式、寬高等等!