1

I have a little shell script which needs to stop a service. This needs to work on ubuntu (14.04), debian and Arch. Right now what I do is similiar to

   case $(cat /etc/issue) in 
     *Ubuntu*)
       service command
     *Debian*)
       /etc/init.d/servicename command
     *Arch*)
       systemctl service command
   esac

Is there any better way to do this?

1
  • Just wait a few years. They're all switching to systemd :) May 19, 2014 at 13:52

4 Answers 4

5

I would say don't look at the current distro to determine which init system to use to manage daemons:

  • It's a little nebulous to determine a distro (as described in Dennis's answer)
  • the init system in use changes between versions of a distro (as Michael Hampton noted, the big name distros are all gravitating toward systemd; Ubuntu is currently the one big-name holdout, and they plan to switch over by 2016).
  • The init system may be changed on an individual installation from the default (starting with 14.10 Utopic Unicorn this October, some Ubuntu installations may elect to switch over to systemd ahead of time).

Although this isn't foolproof (a system may elect to install multiple init systems alongside each other), the way I would do it is to look for the presence of the tools themselves:

if command -v systemctl >/dev/null; then
  # assume systemd
  systemctl $command $servicename
elif command -v initctl >/dev/null; then
  # assume upstart
  initctl $command $servicename
elif command -v service >/dev/null; then
  # assume old Debian `service` utility
  service $servicename $command

# ... elif cases for any other utils you'd want to check for ...

else
  # assume bare init.d scripts
  /etc/init.d/$servicename $command
fi

This answer to a similar question describes some further heuristics you can use to determine the init system currently in use, for cases where this isn't accurate enough.

Ultimately, though, there's no one way to truly detect which one init system an installation "uses" - it's possible to create a setup that switches between init systems every time it boots.

2

i would not go with /etc/issue as this file is sometimes changed or might not even mention the Version.

instead you should in my opinion search for the distribution specific file.

For your part:

Debian: /etc/debian_version
Ubuntu: /etc/lsb-release
Arch: /etc/arch-release

personally i would check for the file existence and "case" it based on that file

see here for some of the other distribution files

linuxmafia.com

1
  • 1
    In newer versions of modern distros, you can check the ID and ID_LIKE fields of /etc/os-release to determine the current distro (and map it to a list of distros you expect/understand). Aug 27, 2014 at 0:12
2

If you have it around for some other reason, facter has a operatingsystem fact that would do what you want.

1

Both Ubuntu and debian have the service utility. Use service servicname start|stop for everything but arch.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .