systemd 下supervisord服务开机自启动

centos7 开机自启动脚本:

#vim /lib/systemd/system/supervisord.service

# supervisord service for sysstemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)

[Unit]
Description=Supervisor daemon

[Service]
ExecStart=/usr/bin/supervisord
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

这个自启动脚本需要修改/etc/supervisord.conf配置文件:

    #vim /etc/supervisrod.conf
        nodaemon=false    改成true

或者:#vim /lib/systemd/system/supervisord.service

[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
SysVStartPriority=99

[Install]
WantedBy=multi-user.target

无需修改/etc/supervisord.conf配置文件

两个自启动脚本都能够添加到systemctl自启动服务

#systemctl enable supervisord.service
#systemctl start/restart/stop supervisor.service

systemd 下supervisor服务自启动服务管理有变量的服务

但是当superviosr被管理的服务有环境变量,是会有问题(比如启动elasticserch服务,有JAVA_HOME和limit.conf的依赖),原因:/etc/profile或者/etc/security/limit.conf 这些文件中配置的环境变量仅对通过pam登录的用户生效,
而systemd是不读这些配置的,所以这就造成登录到终端时查看环境变量和手动启动应用都一切正常,但是systemd无法正常启动应用

获取可以通过添加环境变量来解决(未测试,因为supervisor服务自启动的需要不是很强烈):
[Service]
environment=PYTHONPATH=/opt/mypypath:%(ENV_PYTHONPATH)s,PATH=/opt/mypath:%(ENV_PATH)s

参考:https://github.com/Supervisor/initscripts
参考:https://blog.csdn.net/binggoogle/article/details/53203991
参考:https://www.cnblogs.com/mhc-fly/p/8512491.html
参考:http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html