Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Supervisord on linux CentOS 7 only works when run with root

Tags:

linux

I am trying to run a process in the background as a deamon but it only works when I use root as user.

This is what I did.

Installed supervisor as told on their website

$ yum -y install python-setuptools

$ easy_install supervisor

created the config folders

$ mkdir -p /etc/supervisor/conf.d

populate with default settings

$ echo_supervisord_conf > /etc/supervisor/supervisord.conf

add a new user

$ useradd gogopher

on CentOS 7 to make it start automatically I had to do this

$ vim /usr/lib/systemd/system/supervisord.service

added the code below

[Unit]                                                              
Description=supervisord - Supervisor process control system for UNIX
Documentation=http://supervisord.org                                
After=network.target                                                

[Service]                                                           
Type=forking                                                        
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf             
ExecReload=/usr/bin/supervisorctl reload                            
ExecStop=/usr/bin/supervisorctl shutdown                            
User=gogopher

[Install]                                                           
WantedBy=multi-user.target                                                  

now I can enable it so that it starts on reboot. this all works fine.

$ systemctl enable supervisord

$ systemctl start supervisord

$ systemctl status supervisord

OK

editing the config file to include files from conf.d folder

$ vim /etc/supervisor/supervisord.conf

adding at the end of file

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

adding a simple program

$ vim /etc/supervisor/conf.d/goapp.conf

[program:main]
command=/srv/www/websiteurl.com/bin/main
autostart=true
autorestart=true
startretries=10
user=gogopher

$ systemctl restart supervisord

no error, but the process does not work

if I reboot nothing happens

$ systemctl status supervisord

shows that it supervisord is running but not the daemon program.

if I run

$ supervisorctl reload

I get the error

error: <class 'socket.error'>, [Errno 111] Connection refused: file: /usr/lib64/python2.7/socket.py line: 571

if I run

$ supervisorctl status main

I get the error

http://localhost:9001 refused connection

I have already disabled selinux.

but the weird part is that if I change both of them to root, it works.

The executable is able to be executed by user group and others.

So I have no idea what is going on. I have heard that I should not use root as user that is running a webserver for security reasons.

like image 483
Alex Avatar asked Jul 01 '15 09:07

Alex


1 Answers

For all the people out there having the same problem, this works for me.

cd 
echo_supervisord_conf > /etc/supervisord.conf
# content of /etc/supervisord.conf ...

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

[inet_http_server] ; inet (TCP) server disabled by default
port=*:9001        ; (ip_address:port specifier, *:port for all iface) - I had all this wrong from my original config.
username=user
password=passwd

Paste this content into /etc/rc.d/init.d/supervisord ( I´m not the owner of this script, by now i don´t remember where i got it from )

#!/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}"
prog_bin="${exec_prefix}/bin/supervisord -c /etc/supervisord.conf"
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

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
like image 181
Oscar Nevarez Avatar answered Sep 22 '22 14:09

Oscar Nevarez