Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to retrieve a list of multiple service statuses in Unix. I'm using the service command: man page.

The statuses all start with the transmission-daemon string, for example.

I require the ability to list multiple services' statuses, with a single command. Here is what I'm currently trying (and failing) with:

Here I'm trying to grab a list of statuses using grep.

service $(ls /etc/init.d | grep "transmission-daemon") status

Here I'm trying to list all statuses, and then grep for them.

service --status-all | grep "transmission-daemon"

This produces the following, which isn't much help:

Multiple transmission daemons via grep and service --status-all

How can I effectively achieve what I require with a single command, so that I can then continue piping to awk for further customisation?

Desired example output:

transmission-daemon started
transmission-daemon2 stopped
transmission-daemon3 started
share|improve this question
up vote 5 down vote accepted

I don't have much to say except that:

  • Your ls | grep thing seems really awkward and wrong to me
  • To solve your grep issue, is it that service outputs to stderr? How about with a redirection of stderr to stdout?

    service --status-all 2>&1 | grep "transmission-daemon"

(but doing this also seems really awkward and wrong to me).

Apparently you want to run your services with the status command, right? How about using like so:


shopt -s nullglob

for s in /etc/init.d/transmission-daemon* ; do
    service "$(basename "$s")" status

In one line:

bash -c 'shopt -s nullglob; for s in /etc/init.d/transmission-daemon* ; do service "$(basename "$s")" status; done'

This gives:

enter image description here

Hope this will put you on the right track (or at least on a better one)!

share|improve this answer
Yes, this example you give works! However you're completely correct - it's really awkward and also takes a while. Any alternatives? +1 for the meantime :) – Jimbo Jul 1 '13 at 21:10
And also, I then have to further figure out the + for started, ? etc. There's got to be another way... – Jimbo Jul 1 '13 at 21:12
@Jimbo please see my edit. – gniourf_gniourf Jul 1 '13 at 21:15
This is nice - is there a way to have this in one command as my question says? That's the goal - off-topic it'll be interpreted by PHP and exec() :) – Jimbo Jul 1 '13 at 21:16
@Jimbo I don't understand your one command requirement. If you consider this a bash script it will be one command. If you want to drive it from php, just exec() directly the service command with the appropriate arguments (here transmission-daemonX status). Or maybe I'm missing something? – gniourf_gniourf Jul 1 '13 at 21:20

Another option would be to use find's -exec option:

-exec command ;
     Execute command; true if 0 status is returned.  All following arguments to
     find are taken to be arguments to the command until an argument consisting 
     of `;' is encountered.  The string '{}' is replaced by the current file 
     name being processed everywhere it occurs in the arguments to the command, 
     not just in arguments where it is alone, as in some versions of find. Both
     of these constructions might need to be escaped (with a '\') or quoted to 
     protect them from expansion by the shell.

Specifically (split for legibility):

find /etc/init.d/ -name "transmission-daemon*" \
  -exec bash -c 'service $(basename "{}") status' \;

And on one line:

find /etc/init.d/ -name "transmission-daemon*" -exec bash -c 'service $(basename "{}") status' \;
share|improve this answer
Hey, thanks for this, it also works well. +1 :) – Jimbo Jul 2 '13 at 8:41
Better written as find /etc/init.d/ -name "transmission-daemon*" -exec bash -c 'service "$(basename "$0")" status' {} \; – gniourf_gniourf Jul 2 '13 at 17:30
@gniourf_gniourf why? as long as the {} is quoted, you should have no problem with weird characters and services won't have spaces in their names. – terdon Jul 2 '13 at 17:46
No you're wrong. If e.g., a filename contains a quote you'll have a surprise. The {} should never be used the way you used it. Moreover, if the filename contains spaces, you'll have problems too, so you should also quote $(basename "$0"). Try it! But then I agree that services are very unlikely to have weird characters. But then it's just a matter of good practices that you apply without even thinking about them when you're used to them ;). – gniourf_gniourf Jul 2 '13 at 17:48
@gniourf_gniourf I have tried it, it works for the services I have installed. I agree with your point in principal but these are init.d services, their names are pretty standard, they should never contain quotes for example because they would not work with service. – terdon Jul 2 '13 at 17:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .