LoginSignup
3
5

More than 5 years have passed since last update.

Python3でSupervisorを動かしてみた、ラズパイでね

Posted at

ことはじめ

ラズパイではPython2.xとPython3.xどちらも最初から使えます。
Python3.xだけ使える環境にしたいのに、Supervisorをインストールしようとすると、Python2.xをインストールしてしまいます。
そこでPython3.xでSupervisorを使えるようにしてみました。
できるだけsudo apt-get install supervisorと同じ環境になるようにしています。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 9.6 (stretch)
Release:    9.6
Codename:   stretch

$ python3 -V
Python 3.5.3

$ python -V

Command 'python' not found, but can be installed with:

sudo apt install python3       
sudo apt install python        
sudo apt install python-minimal

You also have python3 installed, you can run 'python3' instead.
$ sudo apt-get install supervisor
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
以下の追加パッケージがインストールされます:
  libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python
  python-meld3 python-minimal python-pkg-resources python2.7
  python2.7-minimal
提案パッケージ:
  python-doc python-tk python-setuptools python2.7-doc binfmt-support
  supervisor-doc
以下のパッケージが新たにインストールされます:
  libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python
  python-meld3 python-minimal python-pkg-resources python2.7
  python2.7-minimal supervisor
アップグレード: 0 個、新規インストール: 10 個、削除: 0 個、保留: 0 個。
4,382 kB のアーカイブを取得する必要があります。
この操作後に追加で 17.9 MB のディスク容量が消費されます。
続行しますか? [Y/n] n

Supervisorをgit cloneし、インストールします

$ cd
$ git clone https://github.com/Supervisor/supervisor.git
$ cd supervisor
$ sudo python3 setup.py install

ファイルやディレクトリを整えます

コマンドは/usr/local/binにインストールされますが、sudo apt-get install supervisorでは/usr/binなのでシンボリック・リンクを作成します。
また必要なディレクトリを作成します。

$ sudo ln -s /usr/local/bin/echo_supervisord_conf /usr/bin/echo_supervisord_conf
$ sudo ln -s /usr/local/bin/pidproxy /usr/bin/pidproxy
$ sudo ln -s /usr/local/bin/supervisorctl /usr/bin/supervisorctl
$ sudo ln -s /usr/local/bin/supervisord /usr/bin/supervisord

$ sudo mkdir /etc/supervisor
$ sudo mkdir /etc/supervisor/conf.d
$ sudo mkdir /var/log/supervisor

supervisord.confを作成します

デフォルト・コンフィグ・ファイル/etc/supervisor/supervisord.confを作成します。
sudo apt-get install supervisorと全く同じ内容です。

$ sudo vi /etc/supervisor/supervisord.conf
/etc/supervisor/supervisord.conf
; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

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

init.dスクリプトsupervisorを作成し、OS起動時に自動実行されるようにします

/etc/init.d/supervisorを作成します。
sudo apt-get install supervisorと全く同じ内容です。

$ sudo vi /etc/init.d/supervisor
/etc/init.d/supervisor
#! /bin/sh
#
# skeleton      example file to build /etc/init.d/ scripts.
#               This file should be used to construct scripts for /etc/init.d.
#
#               Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#               Modified for Debian
#               by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#               Further changes by Javier Fernandez-Sanguino <jfs@debian.org>
#
# Version:      @(#)skeleton  1.9  26-Feb-2001  miquels@cistron.nl
#
### BEGIN INIT INFO
# Provides:          supervisor
# Required-Start:    $remote_fs $network $named
# Required-Stop:     $remote_fs $network $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop supervisor
# Description:       Start/stop supervisor daemon and its configured
#                    subprocesses.
### END INIT INFO

. /lib/lsb/init-functions

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/supervisord
NAME=supervisord
DESC=supervisor

test -x $DAEMON || exit 0

RETRY=TERM/30/KILL/5
LOGDIR=/var/log/supervisor
PIDFILE=/var/run/$NAME.pid
DODTIME=5                   # Time to wait for the server to die, in seconds
                            # If this value is set too low you might not
                            # let some servers to die gracefully and
                            # 'restart' will not work

# Include supervisor defaults if available
if [ -f /etc/default/supervisor ] ; then
        . /etc/default/supervisor
fi
DAEMON_OPTS="-c /etc/supervisor/supervisord.conf $DAEMON_OPTS"

set -e

running_pid()
{
    # Check if a given process pid's cmdline matches a given name
    pid=$1
    name=$2
    [ -z "$pid" ] && return 1
    [ ! -d /proc/$pid ] &&  return 1
    (cat /proc/$pid/cmdline | tr "\000" "\n"|grep -q $name) || return 1
    return 0
}

running()
{
# Check if the process is running looking at /proc
# (works for all users)

    # No pidfile, probably no daemon present
    [ ! -f "$PIDFILE" ] && return 1
    # Obtain the pid and check it against the binary name
    pid=`cat $PIDFILE`
    running_pid $pid $DAEMON || return 1
    return 0
}

case "$1" in
  start)
        echo -n "Starting $DESC: "
        start-stop-daemon --start --quiet --pidfile $PIDFILE \
                --startas $DAEMON -- $DAEMON_OPTS
        test -f $PIDFILE || sleep 1
        if running ; then
            echo "$NAME."
        else
            echo " ERROR."
        fi
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
        echo "$NAME."
        ;;
  #reload)
        #
        #       If the daemon can reload its config files on the fly
        #       for example by sending it SIGHUP, do it here.
        #
        #       If the daemon responds to changes in its config file
        #       directly anyway, make this a do-nothing entry.
        #
        # echo "Reloading $DESC configuration files."
        # start-stop-daemon --stop --signal 1 --quiet --pidfile \
        #       /var/run/$NAME.pid --exec $DAEMON
  #;;
  force-reload)
        #
        #       If the "reload" option is implemented, move the "force-reload"
        #       option to the "reload" entry above. If not, "force-reload" is
        #       just the same as "restart" except that it does nothing if the
        #   daemon isn't already running.
        # check wether $DAEMON is running. If so, restart
        start-stop-daemon --stop --test --quiet --pidfile $PIDFILE \
        --startas $DAEMON \
        && $0 restart \
        || exit 0
        ;;
  restart)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --retry=$RETRY --quiet --oknodo --pidfile $PIDFILE
        echo "$NAME."
        ;;
  status)
    echo -n "$NAME is "
    if running ;  then
        echo "running"
    else
        echo " not running."
        exit 1
    fi
    ;;
  *)
        N=/etc/init.d/$NAME
        # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
        echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
        exit 1
        ;;
esac

exit 0

OS起動時に自動実行されるようにします。

$ sudo chmod 755 /etc/init.d/supervisor
$ sudo update-rc.d supervisor defaults
$ sudo reboot

必要に応じてディレクトリ/etc/supervisor/conf.dにコンフィグ・ファイルを作成します

動作確認

細かいところまで動作確認していませんが、したいことはできています。

参考

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5