Gentoo Archives: gentoo-dev

From: Tom H <tomh0665@×××××.com>
To: Gentoo Devel <gentoo-dev@l.g.o>
Subject: Re: [gentoo-dev] Providing a `service` scripts that speaks OpenRC and systemd
Date: Sat, 30 Sep 2017 22:56:07
Message-Id: CAOdo=SyatPx7m3egU_z1wXXyXs9HQFULJuh3usS3Ex=dxYLMdQ@mail.gmail.com
In Reply to: Re: [gentoo-dev] Providing a `service` scripts that speaks OpenRC and systemd by Walter Dnes
1 On Sat, Sep 30, 2017 at 12:20 AM, Walter Dnes <waltdnes@××××××××.org> wrote:
2 > On Thu, Sep 28, 2017 at 04:27:31PM -0500, Austin English wrote
3
4
5 >> While having the pleasure of working with some proprietary software
6 >> recently, I was asked to run `service foo restart`, and was surprised to
7 >> see:
8 >> foobar ~ # service foo restart
9 >> * service: service `foo' does not exist
10 >
11 > Ridiculous! We need to develop one universal standard that covers
12 > everyone's use cases. https://xkcd.com/927/
13 >
14 > But if you insist, why not just set up a short bash script called
15 > "service" rather than monkeying with every init system's internals?
16 >
17 > #!/bin/bash
18 > if [[ <condition_running_systemd> ]] ; then
19 > systemctl ${2} ${1}
20 > elif [[ <condition_running_initrc> ]] ; then
21 > /etc/init.d/${1} ${2}
22 > elif [[ <condition_running_some_other_init> ]] ; then
23 > <do whatever that init system requires>
24 > else
25 > echo "ERROR: Unsupported init system; 'service' call failed"
26 > fi
27
28 With a "[ $# -eq 2 ]" test and with "env -i set_some_envvars
29 /etc/init.d/${1} ${2}" (and use "rc-service ${1} ${2}" instead of
30 "/etc/init.d/${1} ${2}")
31
32
33 > This can handle a large number of different inits, with as many "elif"
34 > lines as you care to add. But, how do we reliably detect the currently
35 > running init system? Are there running processes, or entries in /sys/
36 > or /proc/ or /dev that are unique to to each init system?
37
38 It's not init that you want to check, it's rc.
39
40 For openrc, "[ -d /run/openrc ]" should do the trick.
41
42 For systemd, the canonical way is "[ -d /run/systemd/system ]".