Hi Michael,

Quoting Michael Biebl (2012-08-23 04:08:58)
> In addition to invoke-rc.d/update-rc.d it would be great if the
> "service" command had native support for systemd. It already has support
> of upstart, so it should be easy to extend in a similar way for systemd.
A patch is attached to this message. Please have a look and tell me if
you’re okay with it. I’ve tested it on my system and it seems to work
fine.

Best regards,
Michael
--- /usr/sbin/service.O	2012-08-27 22:56:52.201353960 +0200
+++ /usr/sbin/service	2012-08-27 23:33:57.998237177 +0200
@@ -47,6 +47,11 @@
 ACTION=
 SERVICEDIR="/etc/init.d"
 OPTIONS=
+is_systemd=
+
+if [ -e "/sys/fs/cgroup/systemd" ]; then
+    is_systemd=1
+fi
 
 if [ $# -eq 0 ]; then
    echo "${USAGE}" >&2
@@ -79,7 +84,20 @@
                           echo " [ ? ]  $SERVICE" 1>&2
                           continue
                         else
-                          out=$(env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)
+                          if [ -n "$is_systemd" ]; then
+                            # NB: We cannot use systemctl is-active because
+                            # that will also return with exit code 0 for
+                            # scripts such as /etc/init.d/tor
+                            # (sysv-compat-layer) which uses RUN_DAEMON from
+                            # /etc/default/tor and then just exits.
+                            # Intuitively, users don't want to see such
+                            # services as active, plus it doesn't match what
+                            # the init script did, so we check if SubState is
+                            # running.
+                            out=$(systemctl show -p SubState "${SERVICE}.service" 2>&1 | grep 'SubState=running')
+                          else
+                            out=$(env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)
+                          fi
                           if [ "$?" = "0" -a -n "$out" ]; then
                             #printf " %s %-60s %s\n" "[+]" "$SERVICE:" "running"
                             echo " [ + ]  $SERVICE"
@@ -99,8 +117,13 @@
        elif [ $# -eq 2 -a "${2}" = "--full-restart" ]; then
           SERVICE="${1}"
           if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
-            env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" stop
-            env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" start
+            if [ -n "$is_systemd" ]; then
+              env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" systemctl stop "${SERVICE}.service"
+              env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" systemctl start "${SERVICE}.service"
+            else
+              env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" stop
+              env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" start
+            fi
             exit $?
           fi
        elif [ -z "${SERVICE}" ]; then
@@ -131,6 +154,21 @@
    esac
 fi
 
+if [ -n "$is_systemd" ]
+then
+    # We are running on systemd. For supported actions, we directly call
+    # systemctl instead of calling the init script (which diverts to systemctl
+    # due to the lsb-functions diversion anyways).
+    case "${ACTION}" in
+        start|stop|restart|status|reload)
+            exec systemctl "${ACTION}" "${SERVICE}.service"
+        ;;
+        force-reload)
+            exec systemctl restart "${SERVICE}.service"
+        ;;
+    esac
+fi
+
 # Otherwise, use the traditional sysvinit
 if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
    exec env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" ${ACTION} ${OPTIONS}

Reply via email to