在ubuntu中使用supervisor

  • 2019 年 11 月 20 日
  • 筆記

1. 安裝

sudo apt install supervisor

2. 配置一個程式

示例如下,在目錄/etc/supervisor/conf.d/下創建redsocks2.conf,並加入:

[program:redsocks2]  command     = /home/wenfeng/bin/redsocks2 -c /home/wenfeng/conf/redsocks2.conf  autostart = true  autorestart = true  redirect_stderr         = true  stdout_logfile_maxbytes = 50MB  stdout_logfile_backups  = 10  stdout_logfile          = /var/log/supervisor/redsocks2.log
  • [program:x]:配置文件必須包括至少一個program,x是program名稱,必須寫上,不能為空
  • command:包含一個命令,當這個program啟動時執行
  • directory:執行子進程時supervisord暫時切換到該目錄
  • user:賬戶名
  • startsecs:進程從STARING狀態轉換到RUNNING狀態program所需要保持運行的時間(單位:秒)
  • redirect_stderr:如果是true,則進程的stderr輸出被發送回其stdout文件描述符上的supervisord
  • stdout_logfile:將進程stdout輸出到指定文件
  • stdout_logfile_maxbytes:stdout_logfile指定日誌文件最大位元組數,默認為50MB,可以加KB、MB或GB等單位
  • stdout_logfile_backups:要保存的stdout_logfile備份的數量

3. 運行一個程式

如果是剛剛將conf文件放到/etc/supervisor/conf.d/目錄下,重啟supervisor以便識別

1

sudo service supervisor restart

然後啟動我們的程式

1

sudo supervisorctl start awesome

如果supervisor遇到錯誤,可以在/var/log/supervisor/supervisord.log中查看日誌; 如果app運行出現問題,可以在/srv/awesome/log/app.log中查看日誌。

4. 其他

supervisor在運行ssh tunnel時總是退出

[program:gpu_ssh_tunnel]  command     = /usr/bin/ssh  -C2qTnN -D 1080 gpu_name@gpu_domin -i /home/wenfeng/.ssh/id_rsa  autostart = true  autorestart = true  redirect_stderr         = true  stdout_logfile_maxbytes = 50MB  stdout_logfile_backups  = 10  stdout_logfile          = /var/log/supervisor/gpu_ssh_tunnel.log

supervisorctl status顯示

1

gpu_ssh_tunnel FATAL Exited too quickly (process log may have details)

自己構建監控腳本

#!/bin/bash  while true  do  	procnum=`ps -ef| grep 'your_name@your_domin' | grep -v grep | wc -l`  	if [ $procnum -eq 0 ];then  		/usr/bin/ssh  -f -C2qTnN -D 1080 your_name@your_domin -p 88 -i /home/wenfeng/.ssh/id_rsa  	fi  	sleep 30  done

這樣依舊不行,log中顯示Host key verification failed.

最終發現,由於supervisor使用的是root帳號運行ssh,會有一個新的knownhosts,沒有輸入yes接受所以會直接退出。

以下實驗可以看出使用sudo運行時需要重新確認指紋

sudo /usr/bin/ssh  -C2qTnN -D 1080 your_name@your_domin -p your_port -i /home/wenfeng/.ssh/id_rsa  The authenticity of host '[your_name]:88 ([your_ip]:88)' can't be established.  ECDSA key fingerprint is SHA256:M/7Vo2ZjnVVc********DgkZtIjrESKMIaj/rfryfDUmqc.  Are you sure you want to continue connecting (yes/no)? yes

解決方案就是先用root賬戶登錄一下,或者在類似進程中,用supervisor的指定用戶功能。