0

I want to run pstree on a set of pid which I find using ps

ps -aux | grep ^username | awk '{pstree $2}'

Unfortunately, the output is empty, but if I run the pstree command manually with the same pids I get the desired output. What is wrong with the awk command? Or how do I achieve the desired result by other means?

e271p314
  • 3,841
  • 7
  • 36
  • 61
  • As an aside, `man ps` would tell you: Note that __"ps -aux"__ is distinct from __"ps aux"__. – devnull Dec 06 '13 at 14:04
  • 1
    `ps -a -u username -x -o pid` should return the same output as the given pipeline (at least for BSD `ps`; the program varies greatly from platform to platform). – chepner Dec 06 '13 at 14:15
  • What happens if you run pstree inside a C program? I'd imagine the result would be identical to that wouldn't it since neither awk nor C are shell? – Ed Morton Dec 06 '13 at 15:25

3 Answers3

3

Try

  ps -aux | grep ^username | awk '{print $2}' | xargs pstree

(As is, pstree is an empty variable value )

This can can be boiled down to

ps -aux |  awk '/^username/{print $2}' | xargs pstree

IHTH

shellter
  • 36,525
  • 7
  • 83
  • 90
2

use the system function in awk. Also you dont need grep here too

ps -aux | awk '$1=="username"{system("pstree $2")}'
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
1

Use system function in awk like this:

awk '{system("pstree " $2)}'

You can shorten your command to:

ps -aux | awk ' /^username/ { system("pstree " $2) }'
anubhava
  • 761,203
  • 64
  • 569
  • 643