使用supervisor管理一些进程很方便,但是最近由于linux打补丁需要重启,为了系统重启后这些进程也能正常启动,supervisor的自启动就很关键了。
Systemd 的设计目标是,为系统的启动和管理提供一套完整的解决方案。
根据 Linux 惯例,字母d是守护进程(daemon)的缩写。 Systemd 这个名字的含义,就是它要守护整个系统。使用了 Systemd,就不需要再用init了。Systemd 取代了initd,成为系统的第一个进程(PID 等于 1),其他进程都是它的子进程。Systemd 的优点是功能强大,使用方便,缺点是体系庞大,非常复杂。
下面介绍我使用systemd来添加supervisor的开机自启服务。
1、在/lib/systemd/system/中添加supervisord.service文件
[Unit] # 通常是第一个区块,定义Unit元数据信息以及与其他服务的依赖关系
Description=Supervisord Service # 简短描述
[Service] # Service,只有 Service 类型的 Unit 才有这个区块
Restart=on-failure # 定义重启方式: always(总是重启)、on-failure(发生意外退出就重启)
RestartSec=20s # 自动重启当前服务间隔的秒数
User=root
ExecStart=/usr/local/bin/supervisord -n -c /etc/supervisord.conf # 启动某个服务的具体命令
[Install] # 通常是配置文件最后一个区块,定义服务如何启动以及是否开机启动
WantedBy=multi-user.target # 指定一个或多个Target
2、执行以下命令
# 重载所有修改过的配置文件
sudo systemctl daemon-reload
# 如果配置文件里面设置了开机启动,systemctl enable命令相当于激活开机启动
sudo systemctl enable supervisord.service
# 启动supervisor
sudo systemctl start supervisord.service
3、然后重启便能看见supervisor能够自启动
# 重启系统【请确定服务器没有重要程序在运行】
sudo systemctl reboot
# 查看supervisor server运行状态
sudo systemctl status supervisord.service
4、问题
开始我的ExecStart=/usr/local/bin/supervisord -n -c /etc/supervisord.conf 命令中,我没有增加-n参数,因为平时手动启动supervisor没有使用该参数,然后重启服务器后发现supervisor没有成功自启。
通过supervisord -help 查看到-n参数解释为:nodaemon -- run in the foreground (same as 'nodaemon=true' in config file)【Nodaemon——运行在前台(与配置文件中的' Nodaemon =true'相同)】