X-Git-Url: https://git.pld-linux.org/?p=projects%2Frc-scripts.git;a=blobdiff_plain;f=service;h=f882bdb2888fd1d2535ce383b87c03716f4c6ec6;hp=3adfc810b71c289ccae226a8a67f3b50ed2ab798;hb=8903a3b15f08d759d06bb2b02e637e0baf7497bf;hpb=3b917b57dfe4bbc1dc6284d3539071c0b3bbdc2c diff --git a/service b/service index 3adfc810..f882bdb2 100755 --- a/service +++ b/service @@ -4,70 +4,186 @@ PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin" export PATH -VERSION="`basename $0` ver. 0.91" -USAGE="Usage: `basename $0` < option > | --status-all | \ +is_ignored_file() { + case "$1" in + skeleton | README | *.dpkg-dist | *.dpkg-old | rc | rcS | single | reboot | bootclean.sh) + return 0 + ;; + *rpmorig | *rpmnew | *rpmsave | *~ | *.orig) + return 0 + ;; + esac + 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/ 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 TYPE has_systemd + + if [ "$USE_SYSTEMD" != "no" ] && [ -x /bin/systemd_booted ] && /bin/systemd_booted; then + has_systemd=1 + else + unset has_systemd + fi + + cd ${SERVICEDIR} + for SERVICE in *; do + case "${SERVICE}" in + functions | halt | killall | single| linuxconf| kudzu) + ;; + *) + if ! is_ignored_file "${SERVICE}" \ + && [ -x "${SERVICEDIR}/${SERVICE}" ]; then + if [ "$has_systemd" ] && [ -f /lib/systemd/system/${SERVICE}.service ]; then + # D for SystemD + TYPE='D' + else + # S for SysVinit + TYPE='S' + fi + if ! grep -qs "\Wstatus)" "$SERVICE"; then + printf " %s %-60s %s\n" "$TYPE:[?]" "$SERVICE:" "unknown" + continue + else + out=$(env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1) + if [ "$?" = "0" -a -n "$out" ]; then + printf " %s %-60s %s\n" "$TYPE:[+]" "$SERVICE:" "running" + continue + else + printf " %s %-60s %s %s\n" "$TYPE:[-]" "$SERVICE:" "NOT running" + continue + fi + fi + fi + ;; + esac + done +} + +VERSION="$(basename $0) ver. 0.91-pld" +USAGE="Usage: $(basename $0) < option > | --status-all | \ [ service_name [ command | --full-restart ] ]" SERVICE= +USE_SYSTEMD= if [ -d /etc/rc.d/init.d ]; then - SERVICEDIR="/etc/rc.d/init.d" + SERVICEDIR="/etc/rc.d/init.d" else - SERVICEDIR="/etc/init.d" + SERVICEDIR="/etc/init.d" fi if [ $# -eq 0 ]; then - echo "${USAGE}" >&2 - exit 1 + echo "${USAGE}" >&2 + exit 1 fi cd / while [ $# -gt 0 ]; do - case "${1}" in - --help | -h | --h* ) - echo "${USAGE}" >&2 - exit 0 - ;; - --version | -V ) - echo "${VERSION}" >&2 - exit 0 - ;; - *) - if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then - cd ${SERVICEDIR} - for SERVICE in * ; do - case "${SERVICE}" in - functions | halt | killall | single| linuxconf| kudzu | \ - *rpmorig | *rpmnew | *rpmsave | *~ | *.orig) - ;; - *) - if [ -x "${SERVICEDIR}/${SERVICE}" ]; then - env -i LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" status - fi - ;; - esac - done - exit 0 - 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 - exit $? - fi - elif [ -z "${SERVICE}" ]; then - SERVICE="${1}" - else - OPTIONS="${OPTIONS} ${1}" - fi - shift - ;; - esac + case "${1}" in + --help | -h | --h* ) + echo "${USAGE}" >&2 + exit 0 + ;; + --version | -V ) + echo "${VERSION}" >&2 + exit 0 + ;; + --ignore-dependencies) + export SYSTEMCTL_IGNORE_DEPENDENCIES=1 + shift + ;; + --skip-redirect) + export SYSTEMCTL_SKIP_REDIRECT=1 + shift + ;; + --no-systemd) + USE_SYSTEMD=no + shift + ;; + *) + if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then + status_all + exit 0 + 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 + exit $? + fi + elif [ -z "${SERVICE}" ]; then + SERVICE="${1}" + elif [ -z "${ACTION}" ]; then + ACTION="${1}" + else + OPTIONS="${OPTIONS} ${1}" + fi + shift + ;; + esac done -if [ -x "${SERVICEDIR}/${SERVICE}" ]; then - env -i LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" ${OPTIONS} +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 LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" ${ACTION} ${OPTIONS} else - echo "${SERVICE}: unrecognized service" >&2 - exit 1 + echo "${SERVICE}: unrecognized service" >&2 + exit 1 fi