ps -ef | grep java | wc -l


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ps -ef | grep java | wc -l
# 1  
Old 03-24-2009
ps -ef | grep java | wc -l

Hi all i have a syntax , Can some one please give me a script

1, I need to check and execute the command.

ps -ef | grep java | wc -l

5

Output should me 5

if not have to run the command:
ps -ef | grep java

the following java process should be run (java1, java2, java3, java4., java5).

If any of the above process is not runnning that particular daemom must start under the dir
/test/java/scripts/daemons

./startjava1
./startjava2
./startjava3
./startjava4
./startjava5

please need the urgent reply

thaanks
surseh

Last edited by suresh_krish; 03-24-2009 at 01:35 AM..
# 2  
Old 03-24-2009
ps -aef | grep $PATH/startjava1 >/dev/null

if [ $? == 0 ]
then
$PATH/startjava1
echo " started startjava1"
sleep 3
else
echo " no need to start startjava1"
fi


you can use this for your 5 processes
# 3  
Old 03-24-2009
hii thanks for the reply. can u please explain the script. its quite complecated i think. does this covers all my requirements
# 4  
Old 03-24-2009
First the script need to check the java process which should be totally 5, If not have to check with the ps -ef | greo java command, Here this java process should run eg(java1, java2, java3, java4, java5) , IF not have to start the java under the above mentioned directory.. please wating for ur reply
# 5  
Old 03-24-2009
ps -aef | grep $PATH/startjava1 >/dev/null #( this will check if your process is already running if not th $? is 0)

if [ $? == 0 ] (check the return code)
then
$PATH/startjava1 ( if the return code is 0 then start the process)
echo " started startjava1"
sleep 3
else ( if the process is already there the else will be executed)
echo " no need to start startjava1"
fi

===================

similar if then else condotion can be appanded for other 4 procaess after this
# 6  
Old 03-24-2009
Quote:
Originally Posted by amitranjansahu
ps -aef | grep $PATH/startjava1 >/dev/null #( this will check if your process is already running if not th $? is 0)

if [ $? == 0 ] (check the return code)
then
$PATH/startjava1 ( if the return code is 0 then start the process)
echo " started startjava1"
sleep 3
else ( if the process is already there the else will be executed)
echo " no need to start startjava1"
fi

===================

similar if then else condotion can be appanded for other 4 procaess after this
That will not work. See below:

Code:
$ ps -eaf |grep rubbish 1>/dev/null ; echo $?
0
$ ps -eaf |grep java 1>/dev/null ; echo $?
0

Instead, this is one option (there are many ways to do this):

Code:
$ proc=$(ps -eaf |grep rubbish |grep -v grep); [ ! -z "${proc}" ] && echo found
$ proc=$(ps -eaf |grep java |grep -v grep); [ ! -z "${proc}" ] && echo found
found

# 7  
Old 03-24-2009
Quote:
Originally Posted by suresh_krish
First the script need to check the java process which should be totally 5, If not have to check with the ps -ef | greo java command, Here this java process should run eg(java1, java2, java3, java4, java5) , IF not have to start the java under the above mentioned directory.. please wating for ur reply
You can try something like this (I'm not doing any verification of successful deamon start etc. - you do that yourself):

Code:
#!/usr/bin/ksh

i=1
while [ $i -le 5 ];do                   # Go into loop for each java daemon
        prg="startjava$i"              # this will have values like startjava1 for 1st loop and so on
        match=$(ps -eaf |grep "${prg}" |grep -v grep)    # find any processes matching startjava1 and store in variable match
        if [ -z "${match}" ] ; then         # if variable match is empty, startjava1 is not running
                echo "$prg not running. Going to start..."  # self-explanatory
                /test/java/scripts/daemons/$prg  # start the daemon
        else
                echo "$prg already running. Output from ps: $match"   # self-explanatory
        fi
        i=$(( $i + 1 ))      # increment the loop counter
done


Last edited by rikxik; 03-24-2009 at 03:00 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PS -EF | GREP JAVA Modification

Hi, When I do ps -ef | grep java I get the following output on my server, But how to get just the last line and the pid....something like:- wasadmin@ccos2104:TEST:bin> ps -ef | grep java wasadmin 5935 54288 0 12:39 pts/8 00:00:00 grep java ccos2104Cell ccos2104 aa ... (2 Replies)
Discussion started by: crosairs
2 Replies
Login or Register to Ask a Question