If the init system is deterministic then it is highly likely that daemons started by that init system will start with the same pid across reboots, as the same code will be run each boot (though with randomization from the occasional fsck
or selinux
relabels or other such not-on-every-boot code).
However, systemd
on RHEL7 is not deterministic: PIDs are allocated in sequence but systemd
runs tasks in parallel possibly across multiple cores possibly waiting for various hardware or network services. Let's see what happens before and after to the process list:
$ ps axo pid,command | sort -n > before
$ sudo reboot
...
$ ps axo pid,command | sort -n > after
$ comm -12 before after | grep -v \\[
comm: file 1 is not in sorted order
comm: file 2 is not in sorted order
PID COMMAND
1 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
745 /usr/lib/systemd/systemd-logind
864 /usr/sbin/NetworkManager --no-daemon
$
So that's two processes (excepting the obvious not-randomized "PID eins", and kernel threads marked with [
) with the same PID. Across 30 reboots recording much the same information, it appears systemd
is pretty good at randomizing the pids; during those reboots /usr/lib/systemd/systemd-logind
came up at:
PID PERCENTAGE
733 5%
734 5%
737 15%
739 5%
743 5%
746 5%
748 5%
749 5%
752 10%
753 10%
755 5%
758 5%
760 5%
764 5%
771 5%
773 5%
The data was captured with a startup service:
[Unit]
Description=recordpidorder
After=getty.target
[Service]
Type=oneshot
ExecStart=/root/pidandboots
[Install]
WantedBy=multi-user.target
that ran:
#!/bin/bash
NF=/root/sequence
[[ ! -e "$NF" ]] && echo 0 > "$NF"
CUR=$(( 1 + $(< "$NF") ))
ps haxo pid,command > "/root/pidorder$CUR"
[[ $CUR -gt 30 ]] && mv /root/pidandboots /root/pidandboots.done
echo "$CUR" > "$NF"
reboot
Once the system is up and running process creation order will randomize itself as not-at-@reboot
cron jobs fire, users login and run various different commands, etc. This will depend on the system, how many PIDs are created on it, etc.
So yes it is statistically conceivable that a daemon will come up at the same PID on a RedHat system that uses systemd
. However, the odds will vary depending on the hardware and exact services the system offers.