Ubuntu – Does $ service –status-all output to STDERR instead of STDOUT

command lineinit.dservices

This is very unintuitive.

e.g.

When a user tries to:

# service --status-all | grep postgres

This won't work…

The only way to grep `service' is:

# service --status-all 2>&1 | grep postgres

On Gentoo Linux for example this problem does not occur:

# rc-status | grep postgres

will work fine.

Best Answer

It might sound a bit strange but it does not. To be more exact, it displays only unknown [?] statuses on the standard error (besides other error messages).

You can see the script in /usr/sbin/services. The relevant part is the following (lines 68--98):

   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)
              ;;
          *)
            if ! is_ignored_file "${SERVICE}" \
        && [ -x "${SERVICEDIR}/${SERVICE}" ]; then
                    if ! grep -qs "\Wstatus)" "$SERVICE"; then
                      #printf " %s %-60s %s\n" "[?]" "$SERVICE:" "unknown" 1>&2
                      echo " [ ? ]  $SERVICE" 1>&2
                      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" "[+]" "$SERVICE:" "running"
                        echo " [ + ]  $SERVICE"
                        continue
                      else
                        #printf " %s %-60s %s\n" "[-]" "$SERVICE:" "NOT running"
                        echo " [ - ]  $SERVICE"
                        continue
                      fi
                    fi
              #env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status
            fi
            ;;
        esac
      done
      exit 0

The echo " [ + ] $SERVICE" line does not print to standard error, and so the [ - ] variant.

You can easily test the above assumption by running: services --status-all 2>/dev/null. If you have any running service that supports the status command, it will be listed.

Related Question