0

I am trying to fetch the number of threads of a process in a UNIX using command line. After going through the man page of unix command, I learnt that following command:

ps -o nlwp <pid>

returns the number of threads spawned in a process.

Whenever i executed above command in unix, it returned:

NLWP
   7

Now, I want to neglect NLWP and a space before 7.

That is I am just interested in a value, as I will be using it in a script, that I am writing for unit testing?

Is it possible to fetch only value, and neglect everything(Title NLWP, space)?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71
  • can you reopen your recent question? I understood what you were asking and was working on an answer, even though no one else seems to have. – ysth Nov 12 '14 at 18:10

1 Answers1

1

You can always use the --no-headers option in ps to get rid of the headers.

In that case, use awk to just print the first value:

ps --no-headers -o nlwp <pid> | awk '{print $1}'   

Or tr to remove the spaces:

ps --no-headers -o nlwp <pid> | tr -d ' '

If --no-headers is not supported in your ps version, either of these make it:

ps -o nlwp <pid> | awk 'END {print $1}'
ps -o nlwp <pid> | tail -1 | tr -d' '
fedorqui
  • 275,237
  • 103
  • 548
  • 598