The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Operating Systems > AIX
.
google unix.com



AIX AIX is IBM's industry-leading UNIX operating system that meets the demands of applications that businesses rely upon in today's marketplace.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to get and process mysql result set in shell script ashok1979 Shell Programming and Scripting 4 08-03-2009 08:28 AM
Shell script to display top 5 process (per cpu consumption) mr_awd Shell Programming and Scripting 2 05-26-2009 09:23 AM
Shell Script to end process help NoobieBoobie UNIX for Dummies Questions & Answers 6 04-06-2009 03:11 AM
How can i know file using another process on lunix shell script? Tlg13team Shell Programming and Scripting 1 04-02-2007 02:33 AM
Shell Script Dependencies other process pankajkrmishra Shell Programming and Scripting 4 07-25-2006 05:20 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 2 Weeks Ago
haroon_a haroon_a is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 17
Getting process ID in a shell script.

Hi all,

In my script i need to store the process ID of my app server in a variable. I know how to verify whether a process is running, by following:

Code:
 
ps -ef|grep 'jboss'
status=$?
 
if [ "$status" = 0] ; then
   # process is running
else
   # process is nor running
fi
But what I need here to get the process ID of jboss, if its running.

Can someone please advise, how can I get the process ID?

Thanks in advance.
  #2 (permalink)  
Old 2 Weeks Ago
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,042
Code:

PID=$(ps -eo pid,comm | awk '$2 == "jboss" {print $1}')

if [ -z "$PID" ]; then
  ... not running
else
  ... running
fi
Assumes you have just one jboss process running.
  #3 (permalink)  
Old 2 Weeks Ago
haroon_a haroon_a is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 17
Thanks Scott, for quick reply,

Here's what I have in my script:
Code:
#!/bin/sh
. ~/.profile



PID=$(ps -eo pid,comm | awk '$2 == "jboss" {print $1}')

echo $PID

if [ -z "$PID" ]; then
  echo "... not running"
else
  echo "... running"
fi
and I my jboss process is running... i.e.
Code:
haroon_a 544950 659604   1   Nov 02      -  8:30 /app/jboss4.2.0/jdk/bin/java -Dprogram.name=run.sh -server -Xms1024m -Xmx1500m -XX:PermSize=64m -XX:MaxPermSize=256m -Xss512k -XX:+HeapDumpOnOutOfMemory -XX:+DisableExplicitGC -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Djava.endorsed.dirs=/app/jboss4.2.0/lib/endorsed -classpath /apps/shared/jboss4.2.0/bin/run.jar:/app/jboss4.2.0/jdk/lib/tools.jar org.jboss.Main -c DctmServer_MethodServer -b 0.0.0.0
But when I run the script, it shows as "not running", in other workds it doesn't goes to "ELSE" clause.

Can you comment, please.
  #4 (permalink)  
Old 2 Weeks Ago
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,042
Ah, ok, sorry, perhaps a general search:
Code:

PID=$(ps -eo pid,comm | awk '/jboss/ {print $1}')
  #5 (permalink)  
Old 2 Weeks Ago
haroon_a haroon_a is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 17
still the same thing, Scott.

Can you tell me which values does the following compare?

if [ -z "$PID" ]; then
  #6 (permalink)  
Old 2 Weeks Ago
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,042
I don't have jboss running, but if I substitute that for something else then it works fine.

Code:
PID=$(ps -eo pid,comm | awk '/mingetty/ {print $1}')     
/home/oracle/tmp > echo $PID
2499 2500 2501 2502 2503


> cat Test
PID=$(ps -eo pid,comm | awk '/mingetty/ {print $1}')     

echo $PID

if [ -z "$PID" ]; then
  echo no mingetty running
else
  echo alles gut!
fi
-----------------------------
> ./Test
2499 2500 2501 2502 2503
alles gut!
Just saw it's AIX.

Try
Code:
PID=$(ps -ef | awk '/jboss/ {print $2}') 
if [ -z "$PID" ]; then
  echo not running
else
  echo is running!
fi
  #7 (permalink)  
Old 2 Weeks Ago
haroon_a haroon_a is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 17
Quote:
Originally Posted by scottn View Post
I don't have jboss running, but if I substitute that for something else then it works fine.

Code:
PID=$(ps -eo pid,comm | awk '/mingetty/ {print $1}')     
/home/oracle/tmp > echo $PID
2499 2500 2501 2502 2503
 
 
> cat Test
PID=$(ps -eo pid,comm | awk '/mingetty/ {print $1}')     
 
echo $PID
 
if [ -z "$PID" ]; then
  echo no mingetty running
else
  echo alles gut!
fi
-----------------------------
> ./Test
2499 2500 2501 2502 2503
alles gut!
Just saw it's AIX.

Try
Code:
PID=$(ps -ef | awk '/jboss/ {print $2}') 
if [ -z "$PID" ]; then
  echo not running
else
  echo is running!
fi
Scott,

That worked! Thanks alot and real appreciate it.

---------- Post updated at 03:32 PM ---------- Previous update was at 03:11 PM ----------

On a seperate note:

Now I know how to get the processes for my jboss, and store it in a vriable.

I want to issue another ps command on those PIDs of jboss, later on in my script to see if they are still running or not. i.e.

Code:
PID=$(ps -ef | awk '/jboss/ {print $2}') 
if [ -z "$PID" ]; then
  echo not running
else
  echo is running!
fi
...
...
# do some other things here...
...
...
# Now i want to issue a ps command on those process IDs stored in $PID, to see if those process are still runnning.
Can you please advise...

Thanks again.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0