Solved

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

Posted on 2008-10-24
2
4,814 Views
Last Modified: 2013-11-17
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!
0
Comment
Question by:xewoox
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 22800053
Hi,
use  'ps -ef' instead of 'ps uxc'
wmp
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22800057
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)

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now