Setup Supervisor and Python on Centos

Supervisor is a client/server system that allows its users to control a number of processes on UNIX-like operating systems.

Official introduction: http://supervisord.org/introduction.html

Install python

Reference: Python Source Releases

Version 2.7

Install pip

Reference: pip Installation

1
2
wget 'https://bootstrap.pypa.io/get-pip.py'
sudo python get-pip.py

Install Supervisor

Reference: Supervisor Installing

1
sudo pip install supervisor

Config Supervisor

Create default configure file

1
2
3
sudo mkdir -p /etc/supervisor/conf.d
echo_supervisord_conf > supervisord.conf
sudo cp supervisord.conf /etc/supervisor/supervisord.conf

Change supervisord.conf [include] to :

1
2
[include]
files = /etc/supervisor/conf.d/*.conf

Add worker configure file

Example /etc/supervisor/conf.d/example.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
[program:program name]
process_name=%(program_name)s_%(process_num)02d
command=whoami
autostart=true
autorestart=true
startsecs=0
#configure thread number as needed
numprocs=4
redirect_stderr=true
#configure path as needed
stdout_logfile=/var/log/example.log

Start Supervisor

Start supervisord
supervisord -c /etc/supervisor/supervisord.conf

When configure file have updated

1
2
supervisorctl reread
supervisorctl update

Basic operation

1
2
3
4
5
6
7
8
9
supervisorctl status
# start/stop a program
supervisorctl start [program name]
supervisorctl stop [program name]
# start/stop all programs
supervisorctl start all
supervisorctl stop all

Running supervisord Automatically on Startup

Reference:

  1. http://stackoverflow.com/questions/31157928/supervisord-on-linux-centos-7-only-works-when-run-with-root
  2. https://rayed.com/wordpress/?p=1496

Setup init script.

Create /etc/rc.d/init.d/supervisord with the following content, configure line 19 [prefix] and line 20 [config_path] as needed (I don’t know who’s the owner of this script, but thanks all the same):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/sh
#
# /etc/rc.d/init.d/supervisord
#
# Supervisor is a client/server system that
# allows its users to monitor and control a
# number of processes on UNIX-like operating
# systems.
#
# chkconfig: - 64 36
# description: Supervisor Server
# processname: supervisord
# Source init functions
. /etc/rc.d/init.d/functions
prog="supervisord"
prefix="/usr/local/"
exec_prefix="${prefix}"
config_path="/etc/supervisord.conf"
prog_bin="${exec_prefix}/bin/supervisord -c ${config_path}"
PIDFILE="/var/run/$prog.pid"
start()
{
echo -n $"Starting $prog: "
daemon $prog_bin --pidfile $PIDFILE
sleep 1
[ -f $PIDFILE ] && success $"$prog startup" || failure $"$prog startup"
echo
}
stop()
{
echo -n $"Shutting down $prog: "
[ -f $PIDFILE ] && sleep 1 && killproc $prog || success $"$prog shutdown"
echo
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $prog
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
;;
esac

Make the script executable and register it as a service.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
sudo chmod +x /etc/rc.d/init.d/supervisord
sudo chkconfig --add supervisord
sudo chkconfig supervisord on
# Start the service
sudo service supervisord start
# Stop the service
sudo service supervisord stop
# Restart the service
sudo service supervisord restart
# Check service status
sudo service supervisord status