]> git.pld-linux.org Git - projects/rc-scripts.git/blobdiff - service
do not use `:>` which aborts whole script on error
[projects/rc-scripts.git] / service
diff --git a/service b/service
index 5a67a40c65d2f947d354ed703dd379ee64bf1c65..337465709b829e450180dd84e83d44222c857f22 100755 (executable)
--- a/service
+++ b/service
@@ -1,5 +1,4 @@
 #!/bin/sh
-set -x
 
 # Set up a default search path.
 PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
@@ -17,8 +16,63 @@ is_ignored_file() {
        return 1
 }
 
+# check if SERVICE is present in systemd and ACTION is valid systemctl command
+# returns false if systemd is disabled or not active
+is_systemd_service() {
+       local SERVICE=$1 ACTION=$2
+
+       [ "$USE_SYSTEMD" = "no" ] && return 1
+
+       # if we are called from systemd itself, because some .service specified
+       # invocation via /sbin/service. this avoids loops
+       # detect this via CMDLINE var, which has leaked from geninitrd
+       if [ -n "$CMDLINE" ]; then
+               echo >&2  "Warning: CMDLINE env set, likely you are defining .service to use /sbin/service, please use /etc/rc.d/init.d/<SERVICE> instead"
+               return 1
+       fi
+
+       case "$ACTION" in
+       # list obtained as: man systemctl | grep N.*A.*M.*E
+       start | \
+       stop | \
+       reload | \
+       restart | \
+       try-restart | \
+       reload-or-restart | \
+       reload-or-try-restart | \
+       isolate | \
+       kill | \
+       is-active | \
+       status | \
+       show | \
+       reset-failed | \
+       enable | \
+       disable | \
+       is-enabled | \
+       reenable | \
+       preset | \
+       mask | \
+       unmask | \
+       link | \
+       load | \
+       snapshot | \
+       delete | \
+       set-environment | \
+       unset-environment )
+               ;;
+       *)
+               #echo "Not valid systemd command"
+               return 1
+       esac
+
+       [ -x /bin/systemd_booted ] || return 1
+       /bin/systemd_booted || return 1
+
+       /bin/systemctl show "$SERVICE".service | grep -q LoadError= && return 1 || return 0
+}
+
 status_all() {
-       local SERVICE
+       local SERVICE TYPE
        cd ${SERVICEDIR}
        for SERVICE in *; do
                case "${SERVICE}" in
@@ -27,19 +81,26 @@ status_all() {
                *)
                if ! is_ignored_file "${SERVICE}" \
                                && [ -x "${SERVICEDIR}/${SERVICE}" ]; then
+                       if [ -f /etc/init/${SERVICE}.conf ]; then
+                               # U for upstart
+                               TYPE='U'
+                       else
+                               # S for SysVinit
+                               TYPE='S'
+                       fi
                        if ! grep -qs "\Wstatus)" "$SERVICE"; then
                                #printf " %s %-60s %s\n" "[?]" "$SERVICE:" "unknown" 1>&2
-                               echo " [ ? ]  $SERVICE" 1>&2
+                               echo " [ ? ]{$TYPE} $SERVICE"
                                continue
                        else
                                out=$(env -i USE_UPSTART=$USE_UPSTART LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)
                                if [ "$?" = "0" -a -n "$out" ]; then
                                        #printf " %s %-60s %s\n" "[+]" "$SERVICE:" "running"
-                                       echo " [ + ]  $SERVICE"
+                                       echo " [ + ]{$TYPE} $SERVICE"
                                        continue
                                else
                                        #printf " %s %-60s %s\n" "[-]" "$SERVICE:" "NOT running"
-                                       echo " [ - ]  $SERVICE"
+                                       echo " [ - ]{$TYPE} $SERVICE"
                                        continue
                                fi
                        fi
@@ -56,6 +117,7 @@ USAGE="Usage: $(basename $0) < option > | --status-all | \
 
 SERVICE=
 USE_UPSTART=
+USE_SYSTEMD=
 
 if [ -d /etc/rc.d/init.d ]; then
        SERVICEDIR="/etc/rc.d/init.d"
@@ -79,6 +141,14 @@ while [ $# -gt 0 ]; do
                echo "${VERSION}" >&2
                exit 0
                ;;
+       --ignore-dependencies)
+               export SYSTEMCTL_IGNORE_DEPENDENCIES=1
+               shift
+               ;;
+       --skip-redirect)
+               export SYSTEMCTL_SKIP_REDIRECT=1
+               shift
+               ;;
        --upstart)
                USE_UPSTART=yes
                shift
@@ -87,6 +157,10 @@ while [ $# -gt 0 ]; do
                USE_UPSTART=no
                shift
                ;;
+       --no-systemd)
+               USE_SYSTEMD=no
+               shift
+               ;;
          *)
                if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then
                        status_all
@@ -110,7 +184,10 @@ while [ $# -gt 0 ]; do
        esac
 done
 
-if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
+if is_systemd_service "${SERVICE}" "${ACTION}"; then
+       echo >&2 "Redirecting to /bin/systemctl --output=cat ${ACTION} ${SERVICE}.service ${OPTIONS}"
+       exec /bin/systemctl --output=cat ${ACTION} ${SERVICE}.service ${OPTIONS}
+elif [ -x "${SERVICEDIR}/${SERVICE}" ]; then
        exec env -i USE_UPSTART=$USE_UPSTART LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" ${ACTION} ${OPTIONS}
 else
        echo "${SERVICE}: unrecognized service" >&2
This page took 0.029028 seconds and 4 git commands to generate.