Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

if [ command -v systemctl >/dev/null ]
    systemctl service start
    /etc/init.d/service start
share|improve this question

marked as duplicate by jasonwryan, Gilles, slm, terdon, Faheem Mitha Mar 26 '14 at 21:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

That 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. – phoops Mar 26 '14 at 20:46
Unfortunately, there is no clean, surefire way of doing this. You should also have a look through this Q&A for some possible workarounds. – terdon Mar 26 '14 at 21:27
up vote 15 down vote accepted

Systemd and init have pid = 1

pidof /sbin/init && echo "sysvinit" || echo "other"

Check for systemd

pidof systemd && echo "systemd" || echo "other"

enter image description here

share|improve this answer
When 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. – terdon Mar 26 '14 at 21:23
well, than the other way will be correct? – user55518 Mar 26 '14 at 22:11
No, for example Ubuntu with upstart still reports /sbin/init. Have a look at @slm's answer in the linked dupe for more details. – terdon Mar 26 '14 at 22:15
I meant if checking for systmed as showed last is ok? – user55518 Mar 26 '14 at 22:16
Commenting to say that the check for systemd works on Ubuntu 14.04, 15.04 and 15.10, but not on debian jessie 8.2. – Jose Diaz-Gonzalez Jan 2 at 9:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.