Solved

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

Posted on 2008-10-24
2
4,836 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

914 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

16 Experts available now in Live!

Get 1:1 Help Now