Bash Script monitor process and if two are running kill lowest PID

I am just learning bash scripts and very confused right now. I have a JBoss application that runs and every now and again the application starts acting funny and when I look at top and ps- - ef the process I notice two are running. If I kill off the lower PID the application works as designed. The application is call NaviCron and usually I will run this

#
# This script is used to list NaviCron processes
#
echo "NaviCron process list"
#
ps -ef|grep NaviSysCron
echo " "
echo "------------------------------------"
echo " "
echo "JBoss process list"
ps -ef|grep run.sh
echo " "
echo "------------------------------------"
echo " "
exit 0

So I can see if there are more than one process running and if so I’ll kill the lowest PID manually. I know you can do just about anything with scripts so what is the easiest and most efficient way to script this so if there are two processes the script would automagically kill off the lower PID?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Without providing the answer you’re after, keep in mind that the ‘lowest
PID’ doesn’t really mean anything with regard to startup order unless you
haven’t had enough processes to wrap around from the max down to the low
unused numbers again. A better (and probably more-complex) way of doing
this would be to kill either the older process or the newer process,
depending on which one is bad. You may also want to find which process
does NOT have the socket bound for listening and kill that one.

Anyway, just some thoughts before too much time goes into this.

Good luck.

On 05/09/2011 09:36 AM, D8TA wrote:
>
> I am just learning bash scripts and very confused right now. I have a
> JBoss application that runs and every now and again the application
> starts acting funny and when I look at top and ps- - ef the process I
> notice two are running. If I kill off the lower PID the application
> works as designed. The application is call NaviCron and usually I will
> run this
>
> Code:
> --------------------
> #
> # This script is used to list NaviCron processes
> #
> echo “NaviCron process list”
> #
> ps -ef|grep NaviSysCron
> echo " "
> echo “------------------------------------”
> echo " "
> echo “JBoss process list”
> ps -ef|grep run.sh
> echo " "
> echo “------------------------------------”
> echo " "
> exit 0
>
> --------------------
>
>
> So I can see if there are more than one process running and if so I’ll
> kill the lowest PID manually. I know you can do just about anything with
> scripts so what is the easiest and most efficient way to script this so
> if there are two processes the script would automagically kill off the
> lower PID?
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJNyPe7AAoJEF+XTK08PnB5cjYP/3P916nzck6YDxfsun+Komsg
FI1xZIVdam4Gw4dsdXjjyukfxdbi3PTEZyoAIWP2gE4WaiXfw+Bp7kEDjDWvY9dU
pBwPRQ7DV3rWjpT+SfydRosmwJbav0syl3CqJXix/XpE+oD77/JJdO6JLDGjW2Ni
0bI7k3NDtz3cn4s/SevAiKH6tEXDQJNNCPEalrHefVr85YrqNGOPeyomN4OPZ/8c
RX5g8FvqkxMmI9hfpUHuplztmp4nYpfpS0t1+nuOHSFlQUgtglM3YHDMaJQlYRjF
W3hsP6pURRDm0pFbJeoVcj2PaD/nEcoX0HPbTcQHcG92QW1TArIGUFUu/r6D4z2s
CoPnSjwL5tVor//BixlbqBJp2GYmHhEqkc1obqzSniwb70gqoG3BEAujKRjqQKV1
0IPt9a51DNzS9CySH4FJPe35/gBtuP9Qhc17Z1T4b18fVbC38HE+wJyNEurDYCxq
jsZGeNfDz7VVbfbxW/S4HZpsJJLTJWivVAXtPYT8MNTPcIQiywC9UMHXzRi/ZoqU
jRzJwQGut+7iVP+KKLn26vSNWy4o+lzAyC5vr6VyGpyP/QkNb1Vm5DHhnJ4bjD4b
0nH3O3w4yTJiSo3bwsyk/cTMxhJwlEkE8RKs+pelMLaQ9gi6ip/mE+PAoClw2IR1
Ajv6v6Odve74kp90FdIR
=/UuS
-----END PGP SIGNATURE-----

Hi D8TA,

don’t know whether you already have a solution for your problem and besides the fact ab already wrote, the script you want to run could somehow look like this:

*#!/bin/sh

strProcess=NaviSysCron

while true
do
intProcessid=ps -ef | grep $strProcess | grep -v "grep" | awk '{print $2}' | sort -n | head -n 1
echo $intProcessid # or some kind of (sudo) kill command
#break # if script is started manually and should stop after the kill command
sleep 10 # if script runs as an endless loop
done

exit 0*

Regards.