1

I saw this nice question:

Pass the output of previous command to next as an argument

and tried to get what I wanted from it but no luck.

I often want to watch my maxima/lisp process and instead of running top/htop/etc I thought I could just search for "maxima.core" and use the pid returned from that search as input to the "-p" flag for top. If it returns more than one, it is not so big of a deal.

1.) If I know the PID, I can use this:

top -p 10815 -o S -d 0

2.) To get the PID(s) I do this:

ps -fu <user> | pgrep -f maxima.core

which outputs a list like,

10814
11989

But trying to concatenate them fails for me. My tries have mostly centered around this:

<user>@<user> ~ $ top -o S -d 0 -p $(ps -fu <user> | pgrep -f maxima.core)

Also tried to incorporate the results of this: How can I get the positional parameters, starting from two, or more generally, `n`?

top -o S -d 0 -p "${(ps -fu <user> | pgrep -f maxima.core)[@]}"

but it complained of poor substitution.

I pretty much rely on bash.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
nate
  • 293
  • 3
    Does your version of pgrep support a -d (--delimiter) option? BTW I doubt piping ps -fu <user> into pgrep is doing anything - you probably should use pgrep's own -f and -u options – steeldriver Jan 12 '18 at 04:26
  • aahhh you are right, ps was nothing. Yes, my pgrep supports delimiter - it is from procps-ng 3.3.10. I also know that top's -p will take a comma separated list of PIDs. – nate Jan 12 '18 at 04:30
  • so does top -o S -d 0 -p "$(pgrep -d, -u <user> -f maxima.core)" do the trick? – steeldriver Jan 12 '18 at 04:32
  • yes! it works great - thanks! if you make it an answer I'll accept it – nate Jan 12 '18 at 04:33

1 Answers1

3

If your version of pgrep supports the -d (--delimiter) option, you should be able to use that to provide a comma-separated list of PIDs to top:

top -o S -d 0 -p "$(pgrep -d, -u <user> -f maxima.core)"
steeldriver
  • 81,074
  • -d (not --delimiter) should be fairly portable as it was already there in the original implementation on Solaris 7. If not, you can always pipe to paste -sd, -. – Stéphane Chazelas Jan 12 '18 at 12:31
  • 1
    Note that maxima.core also matches on maxima-core (it's an extended regexp). Use 'maxima\.core' to match on maxima.core only (anywhere in the process argument list). – Stéphane Chazelas Jan 12 '18 at 12:33