1825882 Members
2854 Online
109689 Solutions
New Discussion

Scripting Question

 
SOLVED
Go to solution
SHABU KHAN
Trusted Contributor

Scripting Question

Hi All,

I am executing java command line arguments to check the status of processes in a korn shell script:
example:
java.com.abcdedf check API
The output on stdout is:
API1 RUNNING
API2 RUNNING
API3 RUNNING
API4 PAUSE
.....

I run this in cron every 10 mins and it pages the SAs when the status is other than RUNNING.. One of the APIs let's say API4 goes into a PAUSE status very often and I would like to modify the script to not to email the SA's the first time it goes into PAUSE status but the second time.

Right now everything goes in a file like this
java.... check API > $FILE
grep -v RUNNING $FILE > $FILE2

I later check if this file is more than some value then page with the output ...

Any help will be appreciated..

I am not sure If I was clear enough...

Thanks,
Shabu
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: Scripting Question

Hi:

One simple solution would be to leverage the count option ('-c') of 'grep' to test if there are more than one instance of your target:

if [ `grep -c "API14 PAUSE" /tmp/file` -gtr 1 ]
then
echo "more than one match"
else
echo "only one match"
fi

Regards!

...JRF...
Alan Riggs
Honored Contributor

Re: Scripting Question

if [ $(grep -c PAUSE $FILE2) -gt 0 ]
then
grep -v RUNNING $FILE | grep -v PAUSE > $FILE2
else
grep -v RUNNING $FILE > $FILE2
fi
Jeff Schussele
Honored Contributor

Re: Scripting Question

JRF - that would work if he was appending (>>) - but his grep line isn't.

He'd be better off setting a counter when he sees a pause & resetting it when pause disappears - don't you think?

Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
SHABU KHAN
Trusted Contributor

Re: Scripting Question

Thanks Guys !

Hi JRF, as Jeff pointed out I am not appending the file ...but I get the idea now...

For now, temporarily I will write to two files and then validate..

If you guys could think about a permanent and efficient way to check this that will be great..

I would like to keep this simple (not too many variables/files)..

Thanks again !

-Shabu
Steven Sim Kok Leong
Honored Contributor
Solution

Re: Scripting Question

Hi,

#!/sbin/sh
if grep PAUSE /tmp/javaoutput >/dev/null 2>&1
then
if [ -e /tmp/PAUSED_ONCE ]
then
echo "this is the 2nd match, paging out now ..."
rm -f /tmp/PAUSED_ONCE
else
echo "this is only the 1st match"
touch /tmp/PAUSED_ONCE
fi
fi

Hope this helps. Regards.

Steven Sim Kok Leong
Alan Riggs
Honored Contributor

Re: Scripting Question

Shabu -- if you want a more elegant solution you will have to tell us your "reset condition". At some point, you undoubtedly want to page again on the first new instance of "PAUSE".
SHABU KHAN
Trusted Contributor

Re: Scripting Question


Thanks Steven !

Nice logic :-) I tweaked it a little bit to not to satisfy the first condition if run from the command prompt and also exported variables instead of touch/rm files. Hope that's okay.

-Shabu