example
# install
pip install supervisor
# default config
echo_supervisord_conf > ./supervisord.conf
# supervisord.conf content
[unix_http_server]
file=/tmp/supervisor.sock
[inet_http_server]
port=0.0.0.0:9001
[supervisord]
logfile=/<path>/<to>/supervisord.log
loglevel=info
[program:prog_name]
command=<bash command>
directory=/<workdir>
redirect_stderr=true
stdout_logfile=/<path>/<log>.log
# start on 9001 port
# (default) /etc/supervisord.conf
sudo supervisord -c /<path>/<to>/supervisord.conf
listener config
# /etc/supervisord.conf
[eventlistener:listener]
command=/usr/bin/python3 /path/to/listener.py
process_name=%(program_name)s
numprocs=1
events=PROCESS_STATE
listener.py
# 进程状态变化时会触发
import sys
def write_stdout(s):
# only eventlistener protocol messages may be sent to stdout
sys.stdout.write(s)
sys.stdout.flush()
def write_stderr(s):
sys.stderr.write(s)
sys.stderr.flush()
def main():
while 1:
# transition from ACKNOWLEDGED to READY
write_stdout('READY\n')
# read header line and print it to stderr
line = sys.stdin.readline()
write_stderr(line)
# read event payload and print it to stderr
headers = dict([x.split(':') for x in line.split()])
data = sys.stdin.read(int(headers['len']))
write_stderr(data)
# transition from READY to ACKNOWLEDGED
write_stdout('RESULT 2\nOK')
if __name__ == '__main__':
main()
client
from xmlrpc.client import ServerProxy
server = ServerProxy('http://<supervisord_ip>:9001/RPC2')
print(server.supervisor.getState())
#print(server.system.listMethods())
#print(server.supervisor.getAllProcessInfo())
print(server.supervisor.startProcess(<program_name>))