Google Answers Logo
View Question
 
Q: Process IDs of all processes running in a process group : Linux ? ( No Answer,   1 Comment )
Question  
Subject: Process IDs of all processes running in a process group : Linux ?
Category: Computers > Operating Systems
Asked by: virajpurang-ga
List Price: $30.00
Posted: 16 Apr 2003 11:09 PDT
Expires: 27 Jun 2003 11:05 PDT
Question ID: 191312
How can I list all the java processes (with their process IDS) which
have been started under a given "process group" id on linux (Red Hat
Linux).I need this in order to take thread dumps of Java processes
only in that process group..

Request for Question Clarification by sgtcory-ga on 16 Apr 2003 13:27 PDT
Are you needing this for home use? I found this, and I think I may
even use it :-)


Treeps
http://www.orbit2orbit.com/gmd/tps/treepsfm.html

I had never even heard of this before. I'm an abuser of 'top' and
'ps'. Let me know if we are getting close to what you are looking for.
In the meantime I will leave this question open for clarification, in
case another researcher has a more definite answer.

Thanks!
SgtCory

Request for Question Clarification by denco-ga on 16 Apr 2003 13:40 PDT
Howdy virajpurang!

I am going to post this as a Request for Question Clarification
in the case I have not made the target, or in the case the process
and/or flags do not work on your version of Linux.

These commands have been tested on a Red Hat Linux 7.x box.

If you have a known and unique process group, such as mysql in this
example, that is the only one running the task, you can do:

ps -g mysql

  PID TTY          TIME CMD
 1248 ?        00:00:00 mysqld
 1250 ?        00:00:00 mysqld
 1251 ?        00:00:00 mysqld

If you have a known process group that might be running other processes,
such as root in this example, you can do:

ps -g root | grep sendmail

  PID TTY          TIME CMD
 7362 ?        00:00:00 sendmail
 7463 ?        00:00:00 sendmail
 7471 ?        00:00:00 sendmail
 8052 ?        00:00:00 sendmail
 8208 ?        00:00:00 sendmail
 8244 ?        00:00:00 sendmail

If this works for you, or if you need further work done on this
question, please feel free to ask for clarification.


Search strategy

Personal experience with Linux and some command line testing.

Looking Forward, denco-ga

Clarification of Question by virajpurang-ga on 16 Apr 2003 15:10 PDT
I am using this Perl script :

sub timed_system
{
        local($command,$time) = @_;
        local($ret) = 9999;
 
	if (!defined($child_pid = fork()))
	{
		print("Fork Error\n");
	}
	elsif ( $child_pid )
	{
		# This is the parent.  Wait and kill if necessary.
		# (Eval/alarm structure taken from perlipc man page.)
		eval
		{
			local $SIG{ALRM} = sub { die "timeout!" };
			alarm $time;
			waitpid($child_pid,0);
			$ret = ($? >> 8);
                        print "Child returned $ret following normal
wait\n";
			alarm 0;
		};
		if ($@ and $@ =~ /timeout/)
		{
			# Kill the child and all in it's process group.
			print "TIMEOUT! [$command]  $child_pid\n";
			kill(3,-$child_pid);
            sleep 2;
			kill(9,-$child_pid);
			sleep 2;
			waitpid($child_pid,WNOHANG);
			$ret = 121;
		}
		print "End *** $time [$command] $$ <$ret>\n";
	}
	else
	{
		# This is the child.  Give myself a new process group,
		# then run the command.
		print "Start *** $time [$command] $$\n";
		setpgrp($$);
		exec($command) or print "couldn't exec foo: $! \n";
		print "*********** Should never get here!";
		exit 1;
	}
	
        return $ret;
}

So I have the Process Group id given by $child_pid, now I want the
list of all processes in the Process Group given by this number, so
that I can pick and choose the Java processes that I want to take
Thread dumps of.

Clarification of Question by virajpurang-ga on 16 Apr 2003 15:26 PDT
Also I tried the "ps -g" that you suggested.This essentially does not
work with the unique Process Group id, something that comes closer but
does not answer my question still is "ps -jx".

You wil liunderstand my confusion if you see the differences in the
output of both these commands on my system.

ps jx gives (Third column gives the process group id , and there are
atleast 10 processes with a process group if of 2540 in the listing
given below.)

[ccuser@linux3 wli]$ ps jx | grep java
 2540  2548  2540  2382 ?           -1 SN   55004   3:47 java -cp
:/export/home/
 2548  2549  2540  2382 ?           -1 SN   55004   0:02 java -cp
:/export/home/
 2549  2550  2540  2382 ?           -1 SN   55004   0:21 java -cp
:/export/home/
 2549  2551  2540  2382 ?           -1 SN   55004   0:00 java -cp
:/export/home/
 2549  2552  2540  2382 ?           -1 SN   55004   0:00 java -cp
:/export/home/
 2549  2553  2540  2382 ?           -1 SN   55004   5:25 java -cp
:/export/home/
 2549  2554  2540  2382 ?           -1 SN   55004   0:00 java -cp
:/export/home/
 2549  2555  2540  2382 ?           -1 SN   55004   0:00 java -cp
:/export/home/
 2549  2556  2540  2382 ?           -1 SN   55004   0:03 java -cp
:/export/home/
 2549  2558  2540  2382 ?           -1 SN   55004   0:00 java -cp
:/export/home/
 2549  2559  2540  2382 ?           -1 SN   55004   0:43 java -cp
:/export/home/
    1 15733 15713 15561 ?           -1 S    55004   0:02
/usr/java/j2sdk1.4.0_02
15733 15734 15713 15561 ?           -1 S    55004   0:01
/usr/java/j2sdk1.4.0_02
15734 15735 15713 15561 ?           -1 S    55004   0:21
/usr/java/j2sdk1.4.0_02
15734 15736 15713 15561 ?           -1 S    55004   0:00
/usr/java/j2sdk1.4.0_02
15734 15737 15713 15561 ?           -1 S    55004   0:00
/usr/java/j2sdk1.4.0_02
15734 15738 15713 15561 ?           -1 S    55004   5:53
/usr/java/j2sdk1.4.0_02
15734 15739 15713 15561 ?           -1 S    55004   0:00
/usr/java/j2sdk1.4.0_02
15734 15740 15713 15561 ?           -1 S    55004   0:00
/usr/java/j2sdk1.4.0_02
15734 15741 15713 15561 ?           -1 S    55004   0:02
/usr/java/j2sdk1.4.0_02
15734 15746 15713 15561 ?           -1 S    55004   0:03
/usr/java/j2sdk1.4.0_02
15734 15747 15713 15561 ?           -1 S    55004   0:00
/usr/java/j2sdk1.4.0_02
15734 15753 15713 15561 ?           -1 S    55004   0:00
/usr/java/j2sdk1.4.0_02
15734 15766 15713 15561 ?           -1 S    55004   0:00
/usr/java/j2sdk1.4.0_02
15734 15767 15713 15561 ?           -1 S    55004   0:34
/usr/java/j2sdk1.4.0_02
15734 15768 15713 15561 ?           -1 S    55004   0:00
/usr/java/j2sdk1.4.0_02
15734 15769 15713 15561 ?           -1 S    55004   0:00
/usr/java/j2sdk1.4.0_02
15734 15770 15713 15561 ?           -1 S    55004   0:00
/usr/java/j2sdk1.4.0_02
15734 15771 15713 15561 ?           -1 S    55004   0:28
/usr/java/j2sdk1.4.0_02
 2559 22537  2540  2382 ?           -1 SN   55004   0:02
/usr/java/j2sdk1.4.0_02
 2549 22538  2540  2382 ?           -1 SN   55004   0:00 java -cp
:/export/home/
 2549 22539  2540  2382 ?           -1 SN   55004   0:00 java -cp
:/export/home/
 2549 22540  2540  2382 ?           -1 SN   55004   0:00 java -cp
:/export/home/
 2549 22541  2540  2382 ?           -1 SN   55004   0:00 java -cp
:/export/home/
22537 22542  2540  2382 ?           -1 SN   55004   0:00
/usr/java/j2sdk1.4.0_02
22542 22543  2540  2382 ?           -1 SN   55004   0:00
/usr/java/j2sdk1.4.0_02
22542 22544  2540  2382 ?           -1 SN   55004   0:00
/usr/java/j2sdk1.4.0_02
22542 22545  2540  2382 ?           -1 SN   55004   0:00
/usr/java/j2sdk1.4.0_02
22542 22546  2540  2382 ?           -1 SN   55004   0:02
/usr/java/j2sdk1.4.0_02
22542 22547  2540  2382 ?           -1 SN   55004   0:00
/usr/java/j2sdk1.4.0_02
22542 22548  2540  2382 ?           -1 SN   55004   0:00
/usr/java/j2sdk1.4.0_02
22542 22549  2540  2382 ?           -1 SN   55004   0:00
/usr/java/j2sdk1.4.0_02
22542 22551  2540  2382 ?           -1 SN   55004   0:00
/usr/java/j2sdk1.4.0_02
22542 22552  2540  2382 ?           -1 SN   55004   0:00
/usr/java/j2sdk1.4.0_02
22542 22596  2540  2382 ?           -1 SN   55004   0:00
/usr/java/j2sdk1.4.0_02
22542 22597  2540  2382 ?           -1 SN   55004   0:00
/usr/java/j2sdk1.4.0_02
22542 22598  2540  2382 ?           -1 SN   55004   0:00
/usr/java/j2sdk1.4.0_02
22542 22599  2540  2382 ?           -1 SN   55004   0:00
/usr/java/j2sdk1.4.0_02
14207 26852 26851 14207 pts/1    26851 S    55004   0:00 grep java
[ccuser@linux3 wli]$

but a "ps -g 2540" gives the following 

[ccuser@linux3 wli]$ ps -g 2540
  PID TTY          TIME CMD
[ccuser@linux3 wli]$

Am I missing usage somewhere ?

Request for Question Clarification by denco-ga on 16 Apr 2003 15:42 PDT
Howdy virajpurang!

Try using the following (using your example) instead:

ps -G 2540

Note the capital "G" instead of the lower case "g".  I think you can
only use the local case "g" with actual names of groups (mysql):

ps -g 626
  PID TTY          TIME CMD

ps -G 626
  PID TTY          TIME CMD
25466 pts/1    00:00:00 tcsh
25484 pts/1    00:00:00 ps


Looking Forward, denco-ga

Clarification of Question by virajpurang-ga on 16 Apr 2003 15:45 PDT
Did you see the Perl script that I had added ?

Clarification of Question by virajpurang-ga on 16 Apr 2003 15:49 PDT
I tried that too ...
[ccuser@linux3 wli]$ ps -G 2540
  PID TTY          TIME CMD
was what I got still ...

I have tried ps -f, ps --Group, and ps --group but nothing works.

Clarification of Question by virajpurang-ga on 16 Apr 2003 15:55 PDT
In any cae this is a very cool service that you guys have added ...and
you guys are very helpful to talk to ...

Request for Question Clarification by denco-ga on 16 Apr 2003 16:37 PDT
Howdy virajpurang!

Yep, saw your perl code, but that really isn't my strong suit, so...

I tested some more, and the ps -g (back to lower case) command at least
got some results.

[moviet@robin ~]$ ps ajx | grep mysql
 PPID   PID  PGID   SID TTY   TPGID STAT   UID   TIME COMMAND
 1220  1248   328   328 ?     -1 S      407   0:00 /usr/sbin/mysqld --base
 1248  1250   328   328 ?     -1 S      407   0:00 /usr/sbin/mysqld --base
 1250  1251   328   328 ?     -1 S      407   0:00 /usr/sbin/mysqld --base

[moviet@robin ~]$ ps -g 328 | grep mysqld
  PID TTY          TIME CMD
 1220 ?        00:00:00 safe_mysqld
 1248 ?        00:00:00 mysqld
 1250 ?        00:00:00 mysqld
 1251 ?        00:00:00 mysqld

Perhaps you could do a ps jx and then sort/grep/sed, etc. to get the
results (2540 in the third row, etc.) that you need.

My pleasure! denco-ga

Request for Question Clarification by maniac-ga on 16 Apr 2003 16:46 PDT
Hello Virajpurang,

Another suggestions

Customize the output of ps. From
  http://www.die.net/doc/linux/man/man1/ps.1.html
(or man ps), scroll down to the example of ps -eo. If you just want
pairs of process ID's and process groups, try
  ps -eo pgid,pid
and add -A (for all processes), --user (to select by a specific user),
or other selection criteria. The output in this case will be limited
to just the fields you are looking for.

  --Maniac

Request for Question Clarification by maniac-ga on 14 May 2003 17:29 PDT
Hello Virajpurang,

Did you get the answer you needed? If so, you may want to indicate
that and who should answer the question (so that researcher gets
paid). if not, what more do you need?

  --Maniac
Answer  
There is no answer at this time.

Comments  
Subject: Re: Process IDs of all processes running in a process group : Linux ?
From: chakrapani-ga on 18 Apr 2003 11:52 PDT
 
Hi
  Here is what u can do 
#pstree -cap 

this will give u some thing like this
init,1)
  |-safe_mysqld,751) /usr/bin/safe_mysqld --datadir=/var/lib/mysql ...
  |   `-mysqld,779) --basedir=/ --datadir=/var/lib/mysql --user=mysql ...
  |       `-mysqld,783) --basedir=/ --datadir=/var/lib/mysql --user=mysql ...
  |           `-mysqld,784) --basedir=/ --datadir=/var/lib/mysql ...
  |-screen,10213)
  |   `-bash,10214)
  |-screen,28491)
  |   `-bash,28492)
  |-sendmail,609)
  |-sendmail,603)
  |-sshd,506)
  |   `-sshd,15156)
  |       `-sshd,15158)
  |           `-bash,15159)
  |               `-su,15174) -
  |                   `-bash,15175)
  |                       `-pstree,15189) -cap
  `-syslogd,359)

Now suppose u dont want to see all that ; then do this 
# pstree -cap 15159

then u will get CHhild process of  15159

bash,15159)
  `-su,15174) -
      `-bash,15175)
          `-pstree,15191) -cap 15159

This my processess ...  

If u need to do every time and u dont want to waste time finding ProcessID
then try this script
 
#!/bin/bash
PIDJ=`java some_process 1> /dev/null 2> /dev/null ; echo $$`
pstree -cap $PIDJ

OK In case u decide to watch the whole process how it changes do this

watch -n1 pstree -cap 15159


Hope that helps ... do let me know if this helped

Regards

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy