Convenient way to check if system is using systemd or sysvinit in BASH?
95,132
Systemd and init have pid = 1
pidof /sbin/init && echo "sysvinit" || echo "other"
Check for systemd
pidof systemd && echo "systemd" || echo "other"
Related videos on Youtube
Author by
phoops
Updated on November 26, 2022Comments
-
phoops over 1 year
I am defining common bash files which I want to use across different distributions. I need a way to check if system is using systemd or sysvinit (/etc/init.d/). I need this so I run appropriate command to start the service. What would be safe way to check for this? I currently check for existance of systemctl command, but is that really an option as there might be the case where systemctl command might be available, but it wouldn't necessarily mean that systemd is actually used?
Here is an excerpt from my current bash script:
#!/bin/sh if [ command -v systemctl >/dev/null ] then systemctl service start else /etc/init.d/service start fi
-
Admin about 10 yearsThat question was more related when creating an installer package and the general consensus on the answer was to implement it in a different way. I feel that my question provides a different case, which could provide a different solution/answer to this. I will keep it open for some time and delete it if it does not get any attention.
-
Admin about 10 yearsUnfortunately, there is no clean, surefire way of doing this. You should also have a look through this Q&A for some possible workarounds.
-
-
Admin about 10 yearsWhen will that not echo
sysvinit
? AFAIK, all (or most) init systems will have a PID of 1, that's kind of part of the definition. -
Admin about 10 yearswell, than the other way will be correct?
-
Admin about 10 yearsNo, for example Ubuntu with upstart still reports
/sbin/init
. Have a look at @slm's answer in the linked dupe for more details. -
Admin about 10 yearsI meant if checking for systmed as showed last is ok?
-
Admin about 10 yearsAh, yes, that's better, downvote retracted.
-
Admin over 8 yearsCommenting to say that the check for systemd works on Ubuntu 14.04, 15.04 and 15.10, but not on debian jessie 8.2.
-
Admin about 8 yearsOn my old Debian I get both "sysvinit" and "systemd"
-
Admin over 7 yearsFirst solution is wrong because
/sbin/init
is probably symbolic link to/lib/systemd/systemd
. To check it runfile /sbin/init
. -
Admin over 7 years+1 for
pstree -p
, -1 for the rest of this answer. -
Admin about 7 years-1 for the misleading
pidof init
as it may be a symbolic link tosystemd
. +1 for the comments here that have more than sufficient answers to help who comes here. Try for example:[[ -L "/sbin/init" ]] && echo 'systemd' || echo 'systemV'
-
Admin over 6 yearsActually this doesn't work with Ubuntu. with
pidof systemd
doesn't show the correct systemd id. Rather, it would be useful if you use the following instead:cat /proc/1/status|egrep -i 'name|state'
because systemd replaces sysvinit with the id == 1 in the system process. Hope this helps -
Admin about 5 yearsThe right way to determine whether
systemd
is managing the system is to check for the existence of/run/systemd/system
.