­

在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的指定用户功能。