Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question

marked as duplicate by jasonwryan, Gilles, slm, terdon, Faheem Mitha Mar 26 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. –  edvinas.me Mar 26 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 at 21:27

1 Answer 1

up vote 1 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
1  
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 at 21:23
    
well, than the other way will be correct? –  user55518 Mar 26 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 at 22:15
    
I meant if checking for systmed as showed last is ok? –  user55518 Mar 26 at 22:16
    
Ah, yes, that's better, downvote retracted. –  terdon Mar 26 at 22:23

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