Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In case of one program having multiple instances, running pidof program gives:

`1 2 3`

top -p accepts comma-delimited arguments: 1, 2, 3.

This means that top -p `pidof program` won't work:

    top: unknown argument '1'
usage:  top -hv | -bcisSH -d delay -n iterations [-u user | -U user] -p pid [,pid ...]

Can you show me how to do this. I'm not familiar with awk, sed, etc...

share|improve this question

3 Answers 3

up vote 11 down vote accepted

An alternative to sed for simple things like this is tr:

top -p $(pidof program | tr ' ' ',')

tr can also easily handle a variable number of spaces:

tr -s ' ' ','

Additionally, if you have it available, pgrep can work well here:

top -p $(pgrep -d , program)

Make sure that you leave a space between -d and , as the comma is the argument (the deliminator).

Also, note that pgrep will return every result of "program" so if you have a process called "program-foo," then this will also be returned (hence the name pgrep).

share|improve this answer

sed replaces your with ,:

top -p $(pidof program | sed 's/ /, /g')
share|improve this answer

This works nicely for keeping an eye on more than one program:

top -p `pgrep -d, -f program1`, `pgrep -d, -f program2`
share|improve this answer

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.