dsr
dsr

Reputation: 21

Shell script that takes the PID as the input and get the threads associated with each PID and writes it to a file. Can someone help or guide?

Shell script that takes the PID as the input and get the threads associated with each PID and writes it to a file. Can someone help or guide?

I am using top -H -b -n 1 | grep java > /path/top.log to capture PID's and add them in top.log file

Also I think top -H -p <PID> can help me to get the threads associated with specific PID.

How can I automate it using shell script?

Upvotes: 2

Views: 311

Answers (3)

Armali
Armali

Reputation: 19395

I am using top -H -b -n 1 | grep java > /path/top.log to capture PID's and add them in top.log file

If you just want all java threads: ps -LCjava

Upvotes: 1

oliv
oliv

Reputation: 13259

Get the process name based on name:

pgrep java

Get the thread using the /proc filesystem (on linux kernel)

ls /proc/$(pgrep java)/task

If you have several java PID, use a for loop:

for i in $(pgrep java); do echo $i; ls /proc/$i/task; echo; done

Info: man 5 proc:

/proc/[pid]/task (since Linux 2.6.0-test6)

This is a directory that contains one subdirectory for each thread in the process.

Upvotes: 2

tso
tso

Reputation: 4924

ps -p <PID> -o cmd

and to see pid:

pgrep java

Upvotes: 2

Related Questions