Ritesh Kumar Gupta
Ritesh Kumar Gupta

Reputation: 5191

fetch the value of number of active threads in a process

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)?

Upvotes: 0

Views: 121

Answers (1)

fedorqui
fedorqui

Reputation: 289725

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' '

Upvotes: 1

Related Questions