Link to home
Start Free TrialLog in
Avatar of xewoox
xewoox

asked on

AIX/Linux - How to get pid for a running shell script using ps command

Hi,

I wrote a shell script to find out the process that is running a same command. In the shell script I use

NPID=`ps uxc | grep -i -w "$CName" | awk '{print $2}' | wc -l`

#NPID will return the number of process that is running the command $CName

PID=`ps uxc | grep -i -w "$CName" | awk '{print $2}'

#PID will return the pid of the process that is running the command $CName

This all goes well. However, if the $CName is not an executable but it is a shell script
then the above line won't work. I guess it is because the shell script is not an executable.

Is there anywhere I can get the NPID and PID for shell script?

In fact, for my purpose. I will have multiple process each running the same shell script. So, I want to know how many process are running the same shell script. An I also want to get (using ps command)  their pid.  All these have to be done in a shell script.

Any idea how to do that.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tintin
Tintin

Note that you can write NPID as

NPID=`ps uxc | grep -icw "$CName"`

There should be nothing special about whether the command is a binary or a script.  It should still show up in the list (it does on my system)