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:

 
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.

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.

Thanks Scott, for quick reply,

Here's what I have in my script:

#!/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.

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.

Ah, ok, sorry, perhaps a general search:

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

still the same thing, Scott.

Can you tell me which values does the following compare?

if [ -z "$PID" ]; then

I don't have jboss running, but if I substitute that for something else then it works fine.

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

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.

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.

Perhaps you could put it in a function:

function CheckRunning {
  [ "$(ps -ef | awk '/jboss/ {print $2}')" ] && return 0

  return 1
}

....

CheckRunning || echo ...not running ...
or
CheckRunning && echo ...is running...
or
if [ $(CheckRunning) -eq 0 ]; then
  echo Is Running
else
...
fi
etc.

Thanks Scott, once again...let me try that.

The original problem is in this command:

The option "comm" to "ps" shows shows the name of the process (in this case "java") not the command used to start the process.

This shows the command line (including the string "jboss"), though it could be truncated.

ps -eo pid,args

Actually the original problem was that I completely missed the fact it was AIX. It all went downhill from that point!

Oops. My problem was not reading the posts in the right order.