根据系统文档,子进程分叉后,systemd 将立即报告成功。这与我的快速测试结果一致:
$ cat /etc/systemd/system/test.service
[Service]
Type=simple
ExecStart=/bin/asdfg
$ systemctl daemon-reload
$ systemctl start test # Exits immediately
$ echo $?
0
$ systemctl status test
● test.service
Loaded: loaded (/etc/systemd/system/test.service; static; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2019-12-15 01:56:52 CST; 2s ago
Process: 24483 ExecStart=/bin/asdfg (code=exited, status=203/EXEC)
Main PID: 24483 (code=exited, status=203/EXEC)
Dec 15 01:56:52 iBug-Server systemd[1]: Started test.service.
Dec 15 01:56:52 iBug-Server systemd[24483]: test.service: Failed to execute command: No such file or directory
Dec 15 01:56:52 iBug-Server systemd[24483]: test.service: Failed at step EXEC spawning /bin/asdfg: No such file or directory
Dec 15 01:56:52 iBug-Server systemd[1]: test.service: Main process exited, code=exited, status=203/EXEC
Dec 15 01:56:52 iBug-Server systemd[1]: test.service: Failed with result 'exit-code'.
$
但是,根据我过去的经验,当某些服务启动或重新启动失败时,systemd 会报告这样的失败,例如nginx
或ssh
:
Job for test.service failed because the control process exited with error code.
See "systemctl status test.service" and "journalctl -xe" for details.
为什么 systemd 不报告我的测试服务(与其在 Freedesktop.org 上的手册页匹配)的失败,而是报告其他服务的失败?
澄清:我特指的是命令systemctl start/restart
。通过“报告失败”,它打印出消息并以非零状态退出。的输出systemctl status
无关紧要。