使用环境termux + ubuntu18.04
简介
Supervisor是一个使用Python语言开发的一个用于管理与监控进程的程序。安装可以通过pip方式安装
pip install supervisor
常用指令:
supervisorctl status #查看进程运行状态
supervisorctl start <进程名> #启动进程
supervisorctl stop <进程名> #关闭进程
supervisorctl restart <进程名> #重启进程
supervisorctl update #重新载入配置文件
supervisorctl shutdown #关闭supervisord
supervisorctl clear <进程名> #清空进程日志
supervisorctl #进入到交互模式。使用help查看所有命令。
这里主要是想通过supervisor
实现开机自动执行脚本的功能
知乎上有篇详细介绍supervisor的文章:https://zhuanlan.zhihu.com/p/655433537
使用方法
使用方法可以参考https://zhuanlan.zhihu.com/p/423512485
可以在/etc
目录下查看到如下文件
supervisord.conf
为配置文件supervisor.d/
文件夹为存放配置的地方
示例:
这里是想通过supervisor实现开机时执行/root/Rcanlog/
文件家中mytest.sh
脚本的功能。
mytest.ini
文件的内容如下:
mytest.sh
文件的内容如下:
#!/bin/bash
echo "hello world!" >> ~/canlog/hello.txt
data >> ~/canlog/hello.txt
exit 0
通过supervisorctl start mytest
或supervisorctl update
指令来启动进程。
注意: 在编辑完mytest.sh
和mytest.ini
文件后记得添加可执行权限(chmod +x mytest.sh
或chmod 777 mytest.sh
)
报错分析与解决
分析
会报错的mytest.sh
测试脚本的内容如下:
#!/bin/bash
echo "hello world!" >> ~/canlog/hello.txt
data >> ~/canlog/hello.txt
exit 0
在通过supervisorctl
工具启动自己的测试脚本时会出现如下报错:
通过supervisorctl status
指令查看会出现如下状态:
通过如下指令可以查看报错信息:
supervisorctl tail <program_name> stdout
也可以到对应的日志输出文件中去查找对应的输出信息
实际测试发现在执行supervisor start mytest
指令时,脚本内容实际也执行了,但执行了好几次。
解决
通过测试发现在mytest.sh
脚本文件中exit 0
前面添加sleep 1
便不会再出现上述问题了
#!/bin/bash
echo "hello world!" >> ~/canlog/hello.txt
data >> ~/canlog/hello.txt
sleep 1
exit 0
现在执行启动指令后会显示如下
重启开机后便会自动执行上面的脚本一次。