Getting process ID in a shell script.


 
Thread Tools Search this Thread
Operating Systems AIX Getting process ID in a shell script.
# 1  
Old 11-05-2009
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  
Old 11-05-2009
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  
Old 11-05-2009
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  
Old 11-05-2009
Ah, ok, sorry, perhaps a general search:
Code:

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

# 5  
Old 11-05-2009
still the same thing, Scott.

Can you tell me which values does the following compare?

if [ -z "$PID" ]; then
# 6  
Old 11-05-2009
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  
Old 11-05-2009
Quote:
Originally Posted by scottn
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script Process id

Hi I have one doubt while calling a shell scripts from another shell script. suppose i have two shell script abc.sh and xyz.sh i call many instance of xyz.sh in abc.sh when i look at process id's using ps command i can see process ids for abc.sh command bur not a single process id of xyz.sh.... (2 Replies)
Discussion started by: AnkitMogha
2 Replies

2. Red Hat

How to run a process through shell script.?

hey , i have to write a script to run a process through shell script and if the process runs successfully then return success. i used nohup to start the process but nothing occured . #!/bin/sh nohup ./cvt -f MediationSources.xml & can anyone pls help me Thanks (3 Replies)
Discussion started by: ramsavi
3 Replies

3. Shell Programming and Scripting

Shell script validation using process.

Hi All, I have a shell script in Linux and it will be invoked by 2 methods, 1) An automated background process . 2) Logining into the box and directly executing the script. I need to put a validation in my shell script such that it will be executed successfully on when an... (11 Replies)
Discussion started by: vininx
11 Replies

4. Shell Programming and Scripting

Shell script for process monitoring

Im having a bit of troble coming up with a script that does this monitors processes to see if they die, if they do die, restart the process and write out to a log file that the process was restarted with the new PID and the date and time the new process was launched. Any suggestions? (1 Reply)
Discussion started by: jspinal
1 Replies

5. Shell Programming and Scripting

Terminate a process using shell script

Hi, I am writing a shell script to run a process and write the output of the process to a file. Inside program.sh: ./process.sh > tempfile .. open tempfile do the following But the problem is that process.sh is running indefinitely and program.sh is not executed completely. Can... (4 Replies)
Discussion started by: dreamgirl314
4 Replies

6. Shell Programming and Scripting

Shell script for an db import process

Hello, I would like a to have an script that would accept variables for "foruser" and "touser" during an db import.. I am pasting an sample script below... The "and" in the below script needs to changed..to a correct syntax for i in `cat /tmp/from` and for j in `cat /tmp/to` do {... (2 Replies)
Discussion started by: jjoy
2 Replies

7. Shell Programming and Scripting

Kill a process from parent shell within a shell script

Hi, I am looking for a solution for the following problem: Im Using tcpdump within a shellskript started in a subshell by using brackets: ( /usr/sbin/tcpdump -i ... -c 1 ) - I want the outout of tcpdump saved in a variable - Than tcpdump-Process in the Subshell should be killed - and I... (6 Replies)
Discussion started by: 2retti
6 Replies

8. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

9. UNIX for Dummies Questions & Answers

Shell Script to end process help

I'm trying to write shell script that when invoke, has 2 arguements, or 1. If there's one arguement, then that's the process name. If there's 2, then one is the timeout. USAGE: Assign1.sh <process> <timeout> If no timeout was set, then the default timeout is 15 minutes. So after it read the... (6 Replies)
Discussion started by: NoobieBoobie
6 Replies

10. Shell Programming and Scripting

Shell Script Dependencies other process

Hi I have two queries regarding Shell script: 1. Lets say there are three process, a.sh, b.sh, and c.sh. Now if I have to run c.sh iff a.sh gets over without any error/exception. what would be the code for that. 2. Suppose our shell script is run after running a sql script in ORACLE. Is... (4 Replies)
Discussion started by: pankajkrmishra
4 Replies
Login or Register to Ask a Question