# supervisor not start supervisor启动失败,通过`/var/log/syslog` 直接报错。 ```bash supervisor[2124]: Starting supervisor: systemd[1]: Starting System Logging Service... systemd[1]: Starting Permit User Sessions... zabbix-agent[2122]: zabbix_agentd starting...failed (zabbix_agentd [2144]: /etc/zabbix/zabbix_agentd.d/: [2] No such file or directory). systemd[1]: Started Advanced key-value store. systemd[1]: Started LSB: Start zabbix-agent daemon. systemd[1]: Started LSB: Starts and stops Wicd. systemd[1]: supervisor.service: control process exited, code=exited status=1 systemd[1]: Failed to start LSB: Start/stop supervisor. systemd[1]: Unit supervisor.service entered failed state. ``` 该报错通过systemd控制的sysv init启动脚本报出。从直接报错的代码位置来看是`start-stop-daemon`启动`supervisord` 失败。 ```bash 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 ;; ``` 为什么这里会启动进程失败呢?通过`man start-stop-daemon` 找到了可能的报错原因。 ```bash DESCRIPTION start-stop-daemon is used to control the creation and termination of system-level processes. Using one of the matching options, start-stop-daemon can be configured to find existing instances of a running process. Note: unless --pid or --pidfile are specified, start-stop-daemon behaves similar to killall(1). start-stop-daemon will scan the process table looking for any processes which match the process name, parent pid, uid, and/or gid (if specified). Any matching process will prevent --start from starting the daemon. All matching processes will be sent the TERM signal (or the one specified via --signal or --retry) if --stop is specified. For daemons which have long-lived children which need to live through a --stop, you must specify a pidfile. COMMANDS -S, --start [--] arguments Check for the existence of a specified process. If such a process exists, start-stop-daemon does nothing, and exits with error status 1 (0 if --oknodo is specified). If such a process does not exist, it starts an instance, using either the executable specified by --exec or, if specified, by --startas. Any arguments given after -- on the command line are passed unmodified to the program being started. ``` ```bash 描述: start-stop-daemon 用以控制系统级别进程启动和终止,同时也可以用以配置发现已经存在的进程。 注意:除非指定了--pid 或者--pidfile 选项,否则start-stop-daemon 表现行为类似killall, start-stop-daemon 将扫描所有进程表查找是否匹配进程名,父进程id,uid,和/或 gid。所有匹配的进程将会阻止 - -start 选项启动守护进程。如果指定--stop,所有匹配进程将会通过TERM (除非通过--signal指定其他信号)信号杀死进程。如果通过--stop 结束进程,但是需要保留需要长时间运行的子进程,那么也需要通过--pidfile 指定进程名。 命令: -S, --start [--] arguments 检查指定进程是否存在,如果存在一个进程start-stop-daemon 将不会做任何事情,返回错误状态为1(如果指定 --oknod 将会返回0),如果不存在指定进程id,将会启动守护进程,进程名通过--exec或者--startas 指定,任何在-- 符号后指定的参数将会直接传递给新启动进程实例。 ``` 通过如上分析可以知道这里可能存在了`--pidfile` 指定的进程id,尝试更改`/var/log/supervisord.pid` 文件进程id为1(systemd进程名)。此时发现supervisd再也启动不了了,同时也直接如上报错。 那么这里为什么会出现相同进程id的进程名呢? 通过分析这里设备可能存在异常断电导致设备未正常销毁pid文件,再次启动的时候又检测到相同pid的进程名,所以直接导致了进程的启动失败。 pidfile单例模式启动进程这么通用的机制为什么会出现该缺陷?其他守护进程也会出现?还是只是概率问题。 其实不然,以下分析。 ## sysv init single instance 在[systemd#systemd vs sysv init](id=jaysnote:systemd#systemd_vs_sysv_init) 详细介绍了sysv init 和systemd控制的进程启动。这里的3.0.0 版本supervisor 通过sysv init启动。sysv init通过`start-stop-daemon` 和`--pidfile` 单例模式启动supervisor,升级到高版本的由systemd控制的supervisor启动并不存在该问题。 同时高版本的supervisor(3.3.5) 的pid文件路径从`/var/log/`迁移 `/var/run`了,测试发现该目录会在启动过程中直接清除,所以也就不存在了异常断掉后pid 文件会被销毁的问题。 ```bash $ df -lh Filesystem Size Used Avail Use% Mounted on /dev/mmcblk0p7 6.8G 2.7G 4.2G 39% / tmpfs 489M 38M 452M 8% /run ``` 如上,对于sysv init 出现的supervisor启动失败,直接原因是早期版本把`--pidfile` 文件放在了`/var/log/` 目录,迁移到`/var/run` `tmpfs` 可以解决。也可以选择直接升级高版本(3.3.5)由systemd控制启动的supervisor。 同时,`--pidfile` 文件路径直接用sysvinit (`/etc/init.d/supervisror`) 脚本指定,但是改文件的生产路径通过`/etc/supervisor/supervisord.conf ` 指定。 ```bash $ cat /etc/supervisor/supervisord.conf [supervisord] logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) pidfile=/var/log/supervisord.pid ; (supervisord pidfile;default supervisord.pid) ```