Help with simple script to find PID of process


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with simple script to find PID of process
# 1  
Old 10-30-2012
MySQL Help with simple script to find PID of process

Hi everyone. I've been reading around and am a little bit overwhelmed, hoping to find a kind soul out there to hold my hand through writing my first script. This need has emerged at work and I haven't much experience writing shell scripts, but this is a problem we have with a production environment and I am tasked with providing the solution. After 3 days of staring and trying to Frankenstein together a solution, I'm no closer than I was when I started. I need some help, and so am kindly requesting it, with crossed fingers. I've put on my flame-retardant shield and am admitting I am a complete newbie off the bat. I'm at your mercy!

Here is the situation: we have a java process that keeps crashing. Normally in this situation we would monitor the existence of the pid file to tell us when it is available, but in this case the process is crashing due to a memory fault, which is leaving the pid file stranded. Therefore, it is necessary to periodically run a command, 'jps -l | grep cq' to see if the process is actually available, and interpret the result.

I have a few unique challenges in this case. First, I need to monitor on three different production boxes (all SunOS), but the directory where jps is kept is in two different locations. On one box it is:

Code:
/usr/jdk/jdk1.5.0_24/bin

and the other:

Code:
/usr/jdk/instances/jdk1.5.0/bin

Once I'm able to determine which location the command is to be run from, the command should be run, and if a line is returned then result is 0, if nothing is returned then 1.

It seems so simple but I'm lacking the know-how to put it all together.

I have bits and pieces but I'm sure there are large holes in the script:

Code:
if [-f $CMDPATH1\ $CMDPATH2
THEN
CMDPATH=$CMDPATH1
ELSE
CMDPATH=$CMDPATH2
fi
 
IF [ $PID == 0 ]
THEN
ERROR=1
ELSE
ERROR=0
FI
 
 
CMD="jps -l | grep cq"



So...help? If I left anything out please let me know - I'm very closely monitoring the thread. Thank you in advance. Smilie
# 2  
Old 10-30-2012
Is this what you are trying to do?

Code:
CMDPATH1="/usr/jdk/jdk1.5.0_24/bin"
CMDPATH2="/usr/jdk/instances/jdk1.5.0/bin"

if [ -d $CMDPATH1 ] 
then
      # $CMDPATH1/cmd - Execute your command
elif [ -d $CMDPATH2 ]
then
      # $CMDPATH2/cmd - Execute your command
fi

This User Gave Thanks to Yoda For This Post:
# 3  
Old 10-30-2012
That looks so tidy and nice!

The second part is I guess, how to interpret the output and pass that into a message.

So, the output of the command, if everything is hunky dory, returns the following:

Code:
root@system>/usr/jdk/instances/jdk1.5.0/bin> jps -l | grep cq
11604 cq5-author-4502.jar

If not, nothing comes back. How do I make that input readable? This is such a basic question but I hope I'm in the right place - I need to "echo" this output, and in order to do so need to store the output as a variable, correct?

Thank you for your reply, I really appreciate you helping me get through this!
# 4  
Old 10-30-2012
If you want to check that the process: cq5-author-4502.jar is running and do some action, then you can try:-

Code:
if [ `jps -l | grep cp| wc -l` -eq 0 ]
then
      echo "Process is not running"
      # Or do what you want to do here.
else
      echo "Process is running"
      # Ignore
fi

# 5  
Old 10-30-2012
Thank you!!! Smilie
# 6  
Old 11-05-2012
Code:
#set variables
CMD="jps -l | grep cq | wc -l"
CMDPATH1="/usr/jdk/jdk1.5.0_24/bin/"
CMDPATH2="/usr/jdk/instances/jdk1.5.0/bin/"

if [ -d $CMDPATH1 ]
then
        $CMDPATH1/$CMD
elif [-d $CMDPATH2 ]
then
        $CMDPATH2/$CMD
fi

if [ 'jps -l | grep cq | wc -l' -eq 0 ]
then
        echo "Process is not running"
else
        echo "Process is running"
fi

exit 0

This is what I've got so far. I do get an "invalid argument count" when I run it:

Code:
root@server>/var/opt/OV/bin/instrumentation> ./cq_mon.sh cq_mon
invalid argument count
usage: jps [-help]
       jps [-q] [-mlvV] [<hostid>]

Definitions:
    <hostid>:      <hostname>[:<port>]
./cq_mon.sh[57]: jps -l | grep cq | wc -l: bad number
Process is running

Can anyone tell me what's happening here?
# 7  
Old 11-05-2012
change:
Code:
if [ 'jps -l | grep cq | wc -l' -eq 0 ]

to this:
Code:
if [ `jps -l | grep cq | wc -l` -eq 0 ]

 
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 to report file size, pid and also kill the process

Hi All, Looking for a quick LINUX shell script which can continuously monitors the flle size, report the process which is creating a file greater than certain limit and also kill that process. Can someone please help me on this? (4 Replies)
Discussion started by: vasavimacherla
4 Replies

2. Shell Programming and Scripting

Find the Pid and Kill the Process after a Few Minutes

hi guys i had written a shell script Display Information of all the File Systems i want to find the pid and kill the process after few minutes.how can i obtain the pid and kill it??? sample.sh df -a >> /tmp/size.log and my cron to execute every minute every hour every day * *... (5 Replies)
Discussion started by: azherkn3
5 Replies

3. Shell Programming and Scripting

Find PID for a process

I want to kill a process run by a user of another group. How do I do that..? (3 Replies)
Discussion started by: Haimanti
3 Replies

4. Solaris

how to find PID of a runnign process ?

Hi Friends, How can we find the process ID of a running process using the process name. In AIX I used to use the command "ps -ef | grep <process name>", it used to give me the owner of that process, Process ID and the threads running and the name of the process in the end. However in... (2 Replies)
Discussion started by: sahilsardana
2 Replies

5. Shell Programming and Scripting

find pid of process run in specific location

Hello, I have a process a.out that runs from /a and /b How can I get the pid of the one running from /a ps -C /a/a.out does not work Thanks! (4 Replies)
Discussion started by: JCR
4 Replies

6. Shell Programming and Scripting

grab PID of a process and kill it in a script

#!/bin/sh who echo "\r" echo Enter the terminal ID of the user in use: echo "\r" read TERM_ID echo "\r" ps -t $TERM_ID | grep sh echo "\r" echo Enter the process number to end: echo "\r" read PID echo "\r" kill -9 $PID What this code does is ultimately grab the PID of a users sh... (6 Replies)
Discussion started by: psytropic
6 Replies

7. Shell Programming and Scripting

Find shell process pid launched throug `at`.

Hi. I was testing some staff and wrote simple script, which only writes date to log every 15 seconds. Like that #1.sh while true;do echo `date` >> 1.log sleep 15 done And than i ran this process with `at -s -f 1.sh now`. And now it is running and i don't know how to catch it. I tryed... (1 Reply)
Discussion started by: kukuruku
1 Replies

8. UNIX for Dummies Questions & Answers

How to find the details of the previously running process with PID

OS: Unix or Linux I (only) know the pid of the process which was running earlier (say 5 hrs back) but it is not running now. Is there a way I could find the details of that process? (atleast the name of the process). Please let me know. (2 Replies)
Discussion started by: vijay.d
2 Replies

9. UNIX for Dummies Questions & Answers

Need to get pid of a process and have to store the pid in a variable

Hi, I need to get the pid of a process and have to store the pid in a variable and i want to use this value(pid) of the variable for some process. Please can anyone tell me how to get the pid of a process and store it in a variable. please help me on this. Thanks in advance, Amudha (7 Replies)
Discussion started by: samudha
7 Replies

10. UNIX for Dummies Questions & Answers

Script to kill all child process for a given PID

Is there any build in command in unix to kill all the child process for a given process ID ? If any one has script or command, please let me know. Thanks Sanjay (4 Replies)
Discussion started by: sanjay92
4 Replies
Login or Register to Ask a Question