你不知道的CS模式的程序管理工具,狀態監測、專案啟停一目瞭然!
(摘自百度百科)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的單元格樣式設定,包括字型、樣式、寬高等等!
歡迎關注作者公眾號【Python 集中營】,專注於後端程式設計,每天更新技術乾貨,不定時分享各類資料!