我试图在后台运行一个进程作为deamon但它只有在我使用root作为用户时才有效.
这就是我所做的.
在他们的网站上告知已安装的主管
$yum -y install python-setuptools $easy_install supervisor
创建了配置文件夹
$mkdir -p /etc/supervisor/conf.d
使用默认设置填充
$echo_supervisord_conf > /etc/supervisor/supervisord.conf
$useradd gogopher
在CentOS 7上让它自动启动我必须这样做
$vim /usr/lib/systemd/system/supervisord.service
[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
现在我可以启用它,以便它在重启时启动.一切正常.
$systemctl enable supervisord $systemctl start supervisord $systemctl status supervisord
好
$vim /etc/supervisor/supervisord.conf
[include] files = /etc/supervisor/conf.d/*.conf
添加一个简单的程序
$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重启supervisord
没有错误,但过程不起作用
如果我重启没有任何反应
$systemctl status supervisord
表明它是supervisord正在运行但不是守护程序.
如果我跑
$supervisorctl reload
我收到了错误
error: <class 'socket.error'>,[Errno 111] Connection refused: file: /usr/lib64/python2.7/socket.py line: 571
如果我跑
$supervisorctl status main
我收到了错误
http://localhost:9001 refused connection
我已经禁用了selinux.
但奇怪的是,如果我将它们都改为root,它就可以了.
所以我不知道发生了什么.我听说我不应该使用
root作为出于安全原因运行Web服务器的用户.
解决方法
对于那些有同样问题的人来说,这对我有用.
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
将此内容粘贴到/etc/rc.d/init.d/supervisord(我不是这个脚本的所有者,现在我不记得我从哪里得到它)
#!/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
使脚本可执行并将其注册为服务
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