When running a systemd unit where type=forking and setting a pidfile (example at the end of the description), if the process referred to by the pid in the pidfile exits before systemd has read the file, systemd complains (visible in journald logs) "New main PID <PID> does not exist or is a zombie."
The problem is systemd never records the actual exit status, and querying them from the unit properties shows that the exit status is 0/success even though.
The "Result" property on the unit is "protocol", signifying that we've run afoul of the forking protocol with systemd.
In this case we haven't really broken protocol just that we've exposed a race between monitoring the forked process.
This can happen with any sort of error in the forked process,
Since systemd should be reaping the process anyway, it seems like we should be able to get a correct exit status here.
If there is a small delay between starting the process and the exit then systemd has enough time to attach to the process and monitor correctly.
The properties one would normally check on this process are all zeroed out:
ExecMainStartTimestampMonotonic=0
ExecMainExitTimestampMonotonic=0
ExecMainPID=0
ExecMainCode=0
ExecMainStatus=0
As is the `EXIT_STATUS` environment variable passed along to any "ExecStop" commands.
In some cases I've seen the "stop_time" set in the ExecStart properties of the service, but found this to be unreliable.
I've tried working around this by keeping the control process alive to wait and see if the forked process exits quickly and recording the exit status myself. This is a decent work-around however causes some extra overhead, and seems like it gets into the territory of what I'd expect systemd to do for me.
The example below is just simulating what might happen with a real process that errors out quickly.
Package: systemd
Version: 245.4-4ubuntu3.13
When running a systemd unit where type=forking and setting a pidfile (example at the end of the description), if the process referred to by the pid in the pidfile exits before systemd has read the file, systemd complains (visible in journald logs) "New main PID <PID> does not exist or is a zombie."
The problem is systemd never records the actual exit status, and querying them from the unit properties shows that the exit status is 0/success even though.
The "Result" property on the unit is "protocol", signifying that we've run afoul of the forking protocol with systemd.
In this case we haven't really broken protocol just that we've exposed a race between monitoring the forked process.
This can happen with any sort of error in the forked process,
Since systemd should be reaping the process anyway, it seems like we should be able to get a correct exit status here.
If there is a small delay between starting the process and the exit then systemd has enough time to attach to the process and monitor correctly.
The properties one would normally check on this process are all zeroed out: mestampMonotoni c=0 estampMonotonic =0
ExecMainStartTi
ExecMainExitTim
ExecMainPID=0
ExecMainCode=0
ExecMainStatus=0
As is the `EXIT_STATUS` environment variable passed along to any "ExecStop" commands.
In some cases I've seen the "stop_time" set in the ExecStart properties of the service, but found this to be unreliable.
I've tried working around this by keeping the control process alive to wait and see if the forked process exits quickly and recording the exit status myself. This is a decent work-around however causes some extra overhead, and seems like it gets into the territory of what I'd expect systemd to do for me.
The example below is just simulating what might happen with a real process that errors out quickly.
Example:
cat << EOF > /tmp/systemd- forking- bug.sh
#!/usr/bin/env bash
(
echo \$BASHPID > \$PIDFILE
exit 1
) &
echo control process exiting
EOF
cat << EOF > /tmp/systemd- forking- bug.service /tmp/systemd- forking- bug.pid forking- bug.sh
[Service]
Type=forking
PIDFile=
ExecStart=/bin/bash /tmp/systemd-
EOF
sudo mv /tmp/systemd- forking- bug.service /run/systemd/ system/ forking- bug.service forking- bug.service forking- bug.service
sudo systemctl daemon-reload
sudo systemctl start systemd-
sudo systemctl status systemd-
sudo journalctl --lines=5 -u systemd-