Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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.

share|improve this answer
    
This is great info, thanks, but it still does not answer "why?". The fact that service writes some info to stdout, and some info to stderr, is even more confusing when doing something like service --status-all | less. –  kaliatech Nov 13 '12 at 15:18
1  
@kaliatech The script only outputs to stderr if it finds a service that does not support the status option. It is an error from the script's point of view, because it assumes that every service should adhere to the standard. I believe as time goes by, the number of [?] statuses will be greatly reduced and eventually disappear. –  lgarzo Nov 13 '12 at 17:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.