1823920 Members
3227 Online
109667 Solutions
New Discussion юеВ

Re: ps command

 
SOLVED
Go to solution
Kapil_2
Advisor

ps command

Is there a function to find out the number of instances of a program running on the system?

What system calls does the ps command use to get the process information?

Kapil
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: ps command

Hi Kapil:

I believe 'ps' uses pstat() to obtain much of its information.

...JRF...
John Booth_1
Advisor

Re: ps command

This comand will show you the number of instances of a program running on the system.

ps -ef |grep "program name"
Robin Wakefield
Honored Contributor

Re: ps command

If you've got O'Reilly's Perl Cookbook, you'll find a useful program called psgrep.

Robin
Paula J Frazer-Campbell
Honored Contributor

Re: ps command

Hi
These may help:-

ps -ef|sort ----- output sorted

ps -ef|grep |wc -l ---- output counted

HTH
Paula
If you can spell SysAdmin then you is one - anon
eric stewart_1
Occasional Contributor

Re: ps command

Using the command you suggested will also give the grep command. Here is my px command.
I removes the grep from the list. The only problem I ran into is if you are looking for processes with grep in them.
alias -x px='ps -aef|grep -v grep | grep '
I put it in my .profile and I always have it.

Bill Hassell
Honored Contributor

Re: ps command

Actually, there is a much simpler way to do this. The ps man page lists a number of options that are activated when XPG4 behavior is setup. This is done by simply defining the UNIX95 variable. Something like this:

UNIX95= ps -C prog-name

This is much more efficient than grep'ing through the entire ps list and removing anything with grep. In fact it is the only way to accurately find all copies of grep.

NOTE: Counting the 'true' instances of any program is quite difficult to do accurately. The name of a program (the -C option) comes from the basename of the file containing the program. Any user could create a program called syncer (for instance) and other than the owner's name, it would be difficult to determine just what this program might be. The full pathname of a program is shown in ps only if the program was started that way (another good reason for all root scripts to use full pathnames).

Note also that the line: UNIX95= ps ... is not a typo. The UNIX95= is a quick way to TEMPORARILY assign the env variable UNIX95 for just the duration of the ps command. DON'T do something like:

export UNIX95=
ps -C prog-name

as many system programs and libraries will behave differently (ie, XPG4 behavior) with UNIX95 set. XPG4 also allows process hierarchies to be seen with the -H option.


Bill Hassell, sysadmin
A. Clay Stephenson
Acclaimed Contributor

Re: ps command

Hi Kapil,

ps uses pstat_getproc() to do the major portion of its work. If I were doing it in c I think I would build up a command string and then call popen(). Something like this:

char s_cmd[1024];
char *target_process = "my_process";
FILE *f = NULL;

(void) sprintf(s_cmd,"ps -e | grep %s | grep -v grep",target_process);
f = popen(s_cmd,"r");
if (f != NULL)
{
char *p = NULL,s[512];
int knt = 0;

p = fgets(s,sizeof(s),f);
while (p != NULL)
{
++knt;
.....
p = fgets(s,sizeof(s),f);
}
(void) pclose(f);
(void) printf("%d instances of %s found\n",
knt,target_process);
}

This should be close to what you are looking for, Clay
If it ain't broke, I can fix that.
Kapil_2
Advisor

Re: ps command

Hi James,
That is exactly what I was looking for.
Thanks.

Hi John, Paula, Eric, Bill
Well, actually I wanted to find it out through a program without using the system function to invoke the ps command.
Thanks anyway.

Hi Robin,
I'll check it out.
Thanks.

Hi Clay,
The command pstat_getproc is what I was looking for.
Thanks.
Would the procedure that you mentioned be faster than using the pstat_getproc system call?


Just another clarification.
From the manual page for pstat I understand that when we want to use a shortcut i.e. to get info about a single process, the parameter index should refer to the pid of the process. But otherwise index should not refer to the pid of the process. Then what does index refer to in cases other than the shortcut?

Kapil