有问题
在同一主机上,为什么当我使用kill杀死一个程序ilogtaild时,这个ilogtaild单元仍然处于活动状态?
#systemctl status ilogtaild
● ilogtaild.service - SYSV: ilogtail is log collect agent of Simple Log Service
Loaded: loaded (/etc/rc.d/init.d/ilogtaild; static; vendor preset: disabled)
Active: active (running) since Fri 2017-08-18 12:38:07 CST; 1min 59s ago
Docs: man:systemd-sysv-generator(8)
Process: 33718 ExecStop=/etc/rc.d/init.d/ilogtaild stop (code=exited, status=0/SUCCESS)
Process: 33745 ExecStart=/etc/rc.d/init.d/ilogtaild start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/ilogtaild.service
├─33747 /usr/local/ilogtail/ilogtail
└─33749 /usr/local/ilogtail/ilogtail
#kill -9 33747 33749
#systemctl status ilogtaild
● ilogtaild.service - SYSV: ilogtail is log collect agent of Simple Log Service
Loaded: loaded (/etc/rc.d/init.d/ilogtaild; static; vendor preset: disabled)
Active: active (exited) since Fri 2017-08-18 12:38:07 CST; 2min 9s ago
Docs: man:systemd-sysv-generator(8)
Process: 33718 ExecStop=/etc/rc.d/init.d/ilogtaild stop (code=exited, status=0/SUCCESS)
Process: 33745 ExecStart=/etc/rc.d/init.d/ilogtaild start (code=exited, status=0/SUCCESS)
看这里
#systemctl is-active ilogtaild
active
ilogtaild 配置
#systemctl cat ilogtaild
# /etc/systemd/system/ilogtaild.service
[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/rc.d/init.d/ilogtaild
Description=SYSV: ilogtail is log collect agent of Simple Log Service
Before=runlevel2.target runlevel3.target runlevel4.target runlevel5.target shutdown.target z_nic_irq_set.service muahao.service e2e-qos.service
After=network-online.target network.service khotfix.service
Conflicts=shutdown.target
[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/rc.d/init.d/ilogtaild start
ExecStop=/etc/rc.d/init.d/ilogtaild stop
没问题
#systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2017-08-18 12:43:25 CST; 920ms ago
Main PID: 36532 (crond)
CGroup: /system.slice/crond.service
└─36532 /usr/sbin/crond -n -m/dev/null
#kill -9 36532
#systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: failed (Result: signal) since Fri 2017-08-18 12:43:36 CST; 2s ago
Process: 36532 ExecStart=/usr/sbin/crond -n $CRONDARGS (code=killed, signal=KILL)
Main PID: 36532 (code=killed, signal=KILL)
看这里
#systemctl is-active crond
failed
配置
#systemctl cat crond
# /usr/lib/systemd/system/crond.service
[Unit]
Description=Command Scheduler
After=auditd.service systemd-user-sessions.service time-sync.target
[Service]
EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
[Install]
WantedBy=multi-user.target
答案1
● ilogtaild.service - SYSV:ilogtail 是简单日志服务的日志收集代理 已加载:已加载(/etc/rc.d/init.d/ilogtaild;静态;供应商预设:禁用) …… 文档: man:systemd-sysv-generator(8)
您没有使用明确编写的 systemd 服务单元。您正在使用systemd-sysv-generator
由 van Smoorenburg脚本生成的单元,rc
该脚本缺少 LSB 标头信息。 systemd-sysv-generator
采用一种一刀切的方法,并经过一些猜测进行修改。 van Smoorenburgrc
脚本可能会也可能不会启动长时间运行的守护进程,可能会也可能不会使用 PID 文件,等等。 systemd-sysv-generator
尝试生成适应这些可能性的服务单元。
自动生成的服务单元具有RemainAfterExit=false
或RemainAfterExit=true
根据 van Smoorenburgrc
脚本中的标头信息是否命名 PID 文件。 (如果它命名 PID 文件,RemainAfterExit
则为false
。)
所以:
- 短期内,您
/etc/rc.d/init.d/ilogtaild
应该使用 RedHat 标头来命名 PID 文件pidfile:
。否则,systemd 将继续假设它是一个 van Smoorenburgrc
脚本,不一定会留下长期运行的守护进程。 - 从长远来看,您应该停止使用
/etc/rc.d/init.d/ilogtaild
并停止依赖systemd-sysv-generator
将其包装在随机数服务单元中。
进一步阅读
答案2
应该使用 RemainAfterExit=no
RemainAfterExit=
Takes a boolean value that specifies whether the service shall be considered active even when all its processes exited. Defaults to no.