起初发布在 StackOverflow 上,告诉你过来询问。回顾一下那次谈话——
- 不,我还没有真正能够尝试进行更改以查看会发生什么(可能会影响正在运行的生产服务)。
- RTFM 尽职调查已完成。他们不涉及这个。
我原来的问题如下:
systemd
假设我的单元文件中有以下内容:
Type=forking
Restart=on-failure
父进程退出,状态为 0(子进程成功启动)。在稍后的某个时刻,孩子以非零状态死亡。会发生什么? systemd
可以跟踪子守护进程PID:
Process: 1768 ExecStart=/bin/mydaemon (code=exited, status=0/SUCCESS)
Main PID: 1770 (mydaemon)
是Restart=on-failure
只看parent退出状态,还是也看child?
答案1
我创建了以下单元文件:
[Unit]
Description=Something
[Service]
Type=forking
WorkingDirectory=/tmp
ExecStart=/tmp/script.sh
ExecStop=/tmp/script.sh
Restart=on-failure
script.sh 包含以下内容:
#!/bin/sh
echo "Forking"
/tmp/myscript.sh &
myscript.sh 包含以下内容:
#!/bin/sh
sleep 60
exit 1
果然,每隔 60 秒,systemd 就会重新启动该服务。
如图所示,注意不同的 PID 和开始时间,注意它会重新启动父进程:
linux:~ # systemctl status myService.service
● myService.service - Something
Loaded: loaded (/etc/systemd/system/myService.service; static; vendor preset: disabled)
Active: active (running) since Mon 2017-07-10 20:43:29 CEST; 57s ago
Process: 4393 ExecStart=/tmp/script.sh (code=exited, status=0/SUCCESS)
Main PID: 4396 (script.sh)
Tasks: 2 (limit: 512)
CGroup: /system.slice/myService.service
├─4396 /bin/sh /tmp/script.sh
└─4397 sleep 60
Jul 10 20:43:29 linux.suse systemd[1]: Starting Something...
Jul 10 20:43:29 linux.suse script.sh[4393]: Forking
Jul 10 20:43:29 linux.suse systemd[1]: Started Something.
linux:~ # systemctl status myService.service
● myService.service - Something
Loaded: loaded (/etc/systemd/system/myService.service; static; vendor preset: disabled)
Active: active (running) since Mon 2017-07-10 20:44:29 CEST; 1s ago
Process: 4409 ExecStop=/tmp/script.sh (code=exited, status=0/SUCCESS)
Process: 4417 ExecStart=/tmp/script.sh (code=exited, status=0/SUCCESS)
Main PID: 4420 (script.sh)
Tasks: 2 (limit: 512)
CGroup: /system.slice/myService.service
├─4420 /bin/sh /tmp/script.sh
└─4421 sleep 60
Jul 10 20:44:29 linux.suse systemd[1]: Starting Something...
Jul 10 20:44:29 linux.suse script.sh[4417]: Forking
Jul 10 20:44:29 linux.suse systemd[1]: Started Something.