Search code examples
linuxbashawkpstree

Use pstree inside awk command


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?


Solution

  • Use system function in awk like this:

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

    You can shorten your command to:

    ps -aux | awk ' /^username/ { system("pstree " $2) }'