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:
/usr/jdk/jdk1.5.0_24/bin
and the other:
/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:
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.
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
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!
grep will leave an exit code: 0 if found, 1 if not found. && will execute the next statement, or even list, if found; || will do if not found. The [c] makes grep not find itself; and you may want to be way more specific in the process pattern that you grep for.
Unfortunately that didn't work either. I made the suggested change and get the following:
root@quartz>/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: not found
Process is not running
My concern here is that the process is in fact running...previously it would at least echo the right message. My question is now whether something is wrong with that last 'if' line:
if [ `jps -l | grep cq | wc -l` -eq 0 ]
is being run from the right dir, since the error indicates that jps is not found. :wall:
---------- Post updated at 03:22 PM ---------- Previous update was at 03:10 PM ----------
I appreciate you explaining what each piece does, THANK YOU! I am bumping up against the same problem though, the command jps isn't being found. Somehow I think I need to pass the location of jps from the first part of the script into the bottom.
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: not found
proc not running
Yes. I did that and got a better result - the pid is returned and the correct message comes through: proc running. However, there is still an invalid argument error, and I am still stuck with the problem that this script is to be run on three different systems, two of which have a different absolute path for jps. Result follows:
Are you sure you want to run jps ? From that errror msg I infer it does not report process states etc. but info about remote hosts. Why don't you use ps ?
It is a requirement of my business partner that I use jps. jps will return the information I need, which is basically, is there a cq process running or not. I have confirmed this many times.
The only outstanding issue is that I'm still getting the error, which I believe is because in the last call of jps the directory isn't getting passed correctly. That's what I need help with.