V
V
Valeriy Solovyov2015-02-09 12:00:29
linux
Valeriy Solovyov, 2015-02-09 12:00:29

How to get a list of all services?

Hi everyone
There are two commands in ubuntu:

initctl list
service --status-all

However, getting normal service statuses for processing in scripts is difficult:
started/not started,
autoload enabled/not enabled

While parsing all the services, then individually initctl status ssh/service ssh status — which is not pretty =(
#!/bin/bash
###############
# gether status of services:
#	init.d & upstart
################
# Valeriy Soloviov <il.linkedin.com/pub/valera-solovyov/43/279/80a/en>
# version 0.3 <2.2015>
# - fix stop on initctl
# - added one service detection
# version 0.2 <2.2015>
# - output to json
# version 0.1 <09.2.2015>
# - gether status from init.d 
# - gether status from upstart
###############
SERVICE=
SERVICEDIR="/etc/init.d"
SUDO=""
JSON_OUTPUT="true"
ONLYONESERVICE="false"
usage(){
        cat << EOL
        USAGE:
           $0 -j yes to enable json output [ by default ]
        -s yes  enable sudo [ not used by default ]
          -n servicename (if you want view status of service)

EOL
exit 1
}

while getopts "n:s:j:" option ;do
     case $option in
         j) if ;then
            JSON_OUTPUT="true"
    	    else
        		JSON_OUTPUT="false"
        	    fi
            ;;
            
         n) ONLYONESERVICE="true"
            ONE_SERVICE=$OPTARG
            ;;
            # to use sudo
         s) SUDO=`which sudo`
             ;;
        \?) usage
            exit 1
            ;;
         :) usage
            exit 1
            ;;
     esac
done

# output for one service option
function output_one_service() {
     case "${STATUS}" in
        "?") out_state="unknown";;
        "+")out_state="started";;
        "-")out_state="stopped";;
     esac

    if [ ${JSON_OUTPUT} == "true" ];then
        echo "{ \"${SERVICE}\":"
        echo "  {"
        echo "      \"state\": \"${out_state}\""
        echo "  }"
        #
         echo "}"
    else
         echo "${SERVICE} ${out_state}"
    fi
}

# only if one service we requested
if [ ${ONLYONESERVICE} == "true" ];then
    SERVICE=${ONE_SERVICE}

    STATUS="?"
  ${SUDO} initctl status $SERVICE 2> /dev/null|grep start &> /dev/null 
  if [ $? -eq 0 ];then
    STATUS="+"
  fi
    ${SUDO} initctl status $SERVICE 2> /dev/null|grep stop &> /dev/null
  if [ $? -eq 0 ];then
    STATUS="-"
  fi
    if [ "${STATUS}" != "?" ];then
        output_one_service
        exit 0
    fi


    if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
      if ! ${SUDO} grep -qs "\Wstatus)" "${SERVICEDIR}/$SERVICE"; then
                          #printf " %s %-60s %s\n" "[?]" "$SERVICE:" "unknown" 1>&2
                          #echo "? $SERVICE" 
        STATUS="?"
                      #    continue
      else
        out=$(env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "${SUDO} $SERVICEDIR/$SERVICE" status 2>&1)
                if [ "$?" = "0" -a -n "$out" ]; then
                            #printf " %s %-60s %s\n" "[+]" "$SERVICE:" "running"
                            #echo "+ $SERVICE"
              STATUS="+"
                          #  continue
                 else
                            #printf " %s %-60s %s\n" "[-]" "$SERVICE:" "NOT running"
                            #echo "- $SERVICE"
              STATUS="-"
                           # continue
                 fi
        fi
    fi
    output_one_service
    exit 0
fi


SERVICES_LIST=` ${SUDO} find ${SERVICEDIR}  -type f  -printf "%f\n" |grep -Ev "skeleton|README|*.dpkg-dist|*.dpkg-old|rc|rcS|single|reboot|bootclean.sh|functions|halt|killall|single|linuxconf|kudzu|.legacy-bootordering"`

unset GLOBAL_STATUS
declare -A GLOBAL_STATUS
for SERVICE in ${SERVICES_LIST} ; do
    STATUS="?"
    if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
      if ! ${SUDO} grep -qs "\Wstatus)" "${SERVICEDIR}/$SERVICE"; then
                          #printf " %s %-60s %s\n" "[?]" "$SERVICE:" "unknown" 1>&2
                          #echo "? $SERVICE" 
        STATUS="?"
                      #    continue
      else
        out=$(env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "${SUDO} $SERVICEDIR/$SERVICE" status 2>&1)
                if [ "$?" = "0" -a -n "$out" ]; then
                            #printf " %s %-60s %s\n" "[+]" "$SERVICE:" "running"
                            #echo "+ $SERVICE"
              STATUS="+"
                          #  continue
                 else
                            #printf " %s %-60s %s\n" "[-]" "$SERVICE:" "NOT running"
                            #echo "- $SERVICE"
              STATUS="-"
                           # continue
                 fi
        fi
    fi

    GLOBAL_STATUS[$SERVICE]=${STATUS}
#	echo ">$SERVICE ${GLOBAL_STATUS[$SERVICE]}"
done
for SERVICE in `initctl list|cut -d " " -f1|uniq`;do
  STATUS="?"
  ${SUDO} initctl status $SERVICE 2> /dev/null|grep start &> /dev/null 
  if [ $? -eq 0 ];then
    STATUS="+"
  fi
    ${SUDO} initctl status $SERVICE 2> /dev/null|grep stop &> /dev/null
  if [ $? -eq 0 ];then
    STATUS="+"
  fi
  SERVICE_NAME=`echo $SERVICE|cut -d  " " -f1`
  # check if key exist
    if [ ${STATUS} != "?" ];then
    	if [ ${GLOBAL_STATUS[${SERVICE_NAME}]+abc} ];then 
    		if [ ${GLOBAL_STATUS[${SERVICE_NAME}]} == "?" ];then
    			GLOBAL_STATUS[$SERVICE_NAME]=${STATUS}
    		fi
        else
            GLOBAL_STATUS[$SERVICE_NAME]=${STATUS}
    	fi
  fi
done

START=""
STOP=""
UNKNOWN=""
for SERVICE_NAME in "${!GLOBAL_STATUS[@]}";do
    if [ ${JSON_OUTPUT} == "true" ];then
        if [ ${GLOBAL_STATUS[${SERVICE_NAME}]} == "+" ];then
            START="${START}, \"${SERVICE_NAME}\""
        elif [ ${GLOBAL_STATUS[${SERVICE_NAME}]} == "-" ];then
            STOP="${STOP}, \"${SERVICE_NAME}\""
        else 
            UNKNOWN="${UNKNOWN}, \"${SERVICE_NAME}\""
        fi
    else
      echo "${SERVICE_NAME} ${GLOBAL_STATUS[${SERVICE_NAME}]}"
    fi
done
if [ ${JSON_OUTPUT} == "true" ];then
    echo "{ \"services\":"
    echo "  {"
    
    # started services
    echo "      \"start\": ["
    echo ${START:1}
    echo "      ],"
    echo "      \"stop\": ["
    echo ${STOP:1}
    echo "      ],"
      echo "      \"unknown\": ["
    echo ${UNKNOWN:1}
    echo "      ]"
       
            
    echo "  }"
    #
    echo "}"

fi

cd ${OLDPWD}
exit 0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Romanenko, 2015-02-19
@FessAectan

# aptitude install chkconfig
# chkconfig -l
acpid                     0:off  1:off  2:on   3:on   4:on   5:on   6:off
apache2                   0:off  1:off  2:on   3:on   4:on   5:on   6:off
atd                       0:off  1:off  2:on   3:on   4:on   5:on   6:off
bootlogs                  0:off  1:on   2:on   3:on   4:on   5:on   6:off
bootmisc.sh               0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
checkfs.sh                0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
checkroot-bootclean.sh    0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
checkroot.sh              0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
console-setup             0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
cron                      0:off  1:off  2:on   3:on   4:on   5:on   6:off
dbus                      0:off  1:off  2:on   3:on   4:on   5:on   6:off
exim4                     0:off  1:off  2:on   3:on   4:on   5:on   6:off
hostname.sh               0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
hwclock.sh                0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
isc-dhcp-server           0:off  1:off  2:on   3:on   4:on   5:on   6:off
kbd                       0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
keyboard-setup            0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
killprocs                 0:off  1:on   2:off  3:off  4:off  5:off  6:off
kmod                      0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
motd                      0:off  1:on   2:on   3:on   4:on   5:on   6:off
mountall-bootclean.sh     0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
mountall.sh               0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
mountdevsubfs.sh          0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
mountkernfs.sh            0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
mountnfs-bootclean.sh     0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
mountnfs.sh               0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
mpt-statusd               0:off  1:off  2:on   3:on   4:on   5:on   6:off
mtab.sh                   0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
networking                0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
nfs-common                0:off  1:off  2:on   3:on   4:on   5:on   6:off  S:on 
nfs-kernel-server         0:off  1:off  2:on   3:on   4:on   5:on   6:off
procps                    0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
rc.local                  0:off  1:off  2:on   3:on   4:on   5:on   6:off
rcS                       0:off  1:off  2:off  3:off  4:off  5:off  6:off
rmnologin                 0:off  1:off  2:on   3:on   4:on   5:on   6:off
rpcbind                   0:off  1:off  2:on   3:on   4:on   5:on   6:off  S:on 
rsyslog                   0:off  1:off  2:on   3:on   4:on   5:on   6:off
samba                     0:off  1:off  2:on   3:on   4:on   5:on   6:off
sendsigs                  0:off  1:off  2:off  3:off  4:off  5:off  6:off
ssh                       0:off  1:off  2:on   3:on   4:on   5:on   6:off
tftpd-hpa                 0:off  1:off  2:on   3:on   4:on   5:on   6:off
udev                      0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
udev-mtab                 0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
umountfs                  0:off  1:off  2:off  3:off  4:off  5:off  6:off
umountnfs.sh              0:off  1:off  2:off  3:off  4:off  5:off  6:off
umountroot                0:off  1:off  2:off  3:off  4:off  5:off  6:off
urandom                   0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on

PROFIT!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question