1

I would be happy if the following command produced no output if tail was not running:

ps --no-headers $(pidof tail)

Instead I get:

  964 pts/2    00:00:01 bash
 4393 pts/2    00:00:00 ps
  • 1
    Does your version of ps recognise the -C cmdlist option? – JigglyNaga May 24 '16 at 12:45
  • It does, and it seems to work. – Tshepang May 24 '16 at 12:49
5

If your version of ps supports the -C option:

ps --no-headers -C tail

If not, you can run ps only if pidof succeeds:

pid=$(pidof tail) && ps --no-headers ${pid}

or (for Zsh):

pid=$(pidof tail) && ps --no-headers $=pid

(thanks to Gilles!).

  • In zsh, use ps --no-headers $=pid – Gilles May 25 '16 at 6:16
0

pidof and pgrep are nice commands to determine what's running in a system, but unfortunately both are unavailable on some operating systems. This should work on most flavours of Unix, BSD and Linux:

ps aux | grep tail | grep -v grep

which removes the grep command itself.

0

With pgrep, you can list the PID and the commands, with empty input of no process matches:

pgrep -a tail

If you want more control over what fields are displayed, call pidof or pgrep, then call ps only if the list of processes is non-empty. You can use a temporary variable:

pids=$(pgrep -d, tail)
[ -z "$pids" ] || ps --no-headers -p "$pids"

or ifne from moreutils:

pgrep tail | ifne xargs ps --no-headers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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