Unix & Linux
shell-script init portability
Updated Mon, 18 Jul 2022 12:21:47 GMT

Detect init system using the shell


This may have more to do with detecting operating systems, but I specifically need the init system currently in use on the system.

Fedora 15 and Ubuntu now use systemd, Ubuntu used to use Upstart (long time default until 15.04), while others use variations of System V.

I have an application that I am writing to be a cross-platform daemon. The init scripts are being dynamically generated based on parameters that can be passed in on configure.

What I'd like to do is only generate the script for the particular init system that they are using. This way the install script can be run reasonably without parameters as root and the daemon can be "installed" automagically.

This is what I've come up with:

  • Search for systemd, upstart, etc in /bin
  • Compare /proc/1/comm to the systemd, upstart, etc
  • Ask the user

What would be the best cross/platform way of doing this?

Kind of related, Can I depend on bash to be on the majority of *nix or is it distribution/OS dependent?

Target platforms:

  • Mac OS
  • Linux (all distributions)
  • BSD (all versions)
  • Solaris, Minix, and other *nix



Solution

For the second question, the answer is no and you should have a look at Resources for portable shell programming.

As for the first part - first of all, you certainly have to be careful. I'd say perform several tests to make sure - because the fact that someone does have systemd (for ex.) installed, does not mean it is actually used as the default init. Also, looking at /proc/1/comm can be misleading, because some installations of various init programs can automatically make /sbin/init a symlink hardlink or even a renamed version of their main program.

Maybe the most useful thing could be to look at the init scripts type - because those are what you'll actually be creating, no matter what runs them.

As a side note, you might also have a look at OpenRC which aims to provide a structure of init scripts that is compatible with both Linux and BSD systems.





Comments (5)

  • +2 – What do you mean by "look at the init scripts type"? Often different init systems put their scripts/files somewhere besides /etc/init, like systemd puts them in /etc/systemd. If I was to have my script grok these, it could take a little while. Oh, and thanks for the link BTW for portable shell programming. — Aug 07, 2011 at 00:23  
  • +1 – I meant to say that some init implementations can be adjusted to make use of different init scripts' types and locations (like /etc/rc.d/ or /etc/init.d/). And you should properly adjust your program at installation time to utilize the structure that is used on the given system. — Aug 07, 2011 at 01:08  
  • +2 – So, are you saying that there is no reliable way to detect the init system programmatically? Having the user pass in parameters is certainly safer, but if the user does not pass anything in, what would be the best way to guess? — Aug 07, 2011 at 01:27  
  • +0 – I've no idea what will be the best way for your program - it all depends on what systems will the program end up running. I haven't used too many distributions, but I was trying to share my observations to help you. It is a tough piece of scripting you are about to try and I tried to give you some view on the field - you must come to the final answer yourself or wait for more hints. — Aug 07, 2011 at 01:40  
  • +0 – Cool, thanks for all your help. You definitely have pointed me in the right direction. — Aug 07, 2011 at 01:47  


External Links

External links referenced by this document:

Linked Articles

Local articles referenced by this article: