小白可以用的-Linux背景程序管理利器:supervisor
1.前言:
Linux的背景程序運作有好幾種方法:如nohup、screen等.但是,如果是一個服務程式,要可靠地在背景運作,就需要把它做成daemon,最好還能監控程序狀态,在意外結束時能自動重新開機.
2.supervisor:
supervisor是用Python開發的一套通用的程序管理程式,能将一個普通的指令行程序變為背景daemon,并監控程序狀态,異常退出時能自動重新開機.
3.使用:
(1).安裝supervisor:
apt-get install supervisor
(2).supervisor安裝完成後會生成三個執行程式:
- supervisord:守護程序服務(用于接收程序管理指令)
- supervisorctl:用戶端(用于和守護程序通信,發送管理程序的指令)
- echo_supervisord_conf:生成初始配置檔案程式
(3).使用Laravel隊列,會用到php artisan queue:work指令來監聽隊列.可以給開發的應用程式單獨編寫一個配置檔案,讓supervisor來管理它.每個程序的配置檔案都可以單獨分拆,放在/etc/supervisor/conf.d/目錄下,新增一個Supervisor的配置檔案.以.conf作為擴充名,以下定義了一個lumen-queue的程序:
[program:lumen-queue]
process_name=%(program_name)s_%(process_num)02d
command=php artisan queue:work # 指令
autostart=true
autorestart=true
directory=/var/www/Service # 程序的目前目錄
user=www-data # 程序運作的使用者身份
numprocs=8 # 啟動多少個程序來監聽Laravel隊列
redirect_stderr=true
stdout_logfile=/var/log/supervisor/laravel-queue.log
[program:lumen-queue]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/Service/artisan queue:work redis --daemon --sleep=3 --tries=3
autostart=true
autorestart=true
user=root
numprocs=5
redirect_stderr=true
stdout_logfile=/var/log/supervisor/laravel-queue.log