LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script sometimes work (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-sometimes-work-940076/)

oscarenzo 04-16-2012 12:14 PM

bash script sometimes work
 
Hello guys, i have a problem, i'm working on bash script but the problem is that, only work sometimes, sometimes when i run not work the first condition: follow print the script:

Code:

#!/bin/bash
# Sincronizador para el directorio SOFTWARE

HOST="10.66.64.29"
USER="root"
LOG="/var/log/sync-software.log"
DIR="SOFTWARE"
CMD=`/bin/ps -ef | grep -v grep | grep -c 'sync-software'`
MIN="1"

if [ "$CMD" -gt "$MIN" ] > /dev/null;
        then
                echo "" >> $LOG
                echo "############################" >> $LOG
                echo "Resultado para: $DIR" >> $LOG
                date >> $LOG
                echo "############################" >> $LOG
                echo "No se hace nada por cualquiera de los siguientes motivos:" >> $LOG
                echo "Esta corriendo algun proceso de rsync vinculado a esta tarea" >> $LOG
                echo "Esta corriendo algun proceso de md5sum vinculado a esta tarea" >> $LOG
                echo "" >> $LOG
       
else
                echo "" >> $LOG
                echo "############################" >> $LOG
                echo "Resultado para: $DIR" >> $LOG
                date >> $LOG
                echo "############################" >> $LOG
                echo "Se empieza el proceso de rsync" >> $LOG
                find /home/bcncorp/$DIR/ -type f -print0 | xargs -0 md5sum > /home/bcncorp/md5sum-logs/md5sum-$DIR-actual.log
                #scp -i /root/.ssh/bcncorptosantacorp /home/bcncorp/md5sum-logs/md5sum-bcncorp-financiero.log root@10.66.64.29:/home/santacorp/md5sum-logs/
                if diff /home/bcncorp/md5sum-logs/md5sum-$DIR-nuevo.log /home/bcncorp/md5sum-logs/md5sum-$DIR-actual.log > /dev/null;
                        then
                                echo "No hay diferencias, no se crea nuevo log md5sum y no hace falta sincronizar" >> $LOG
                else
                                echo "Si hay diferencias, se crea el nuevo log md5sum y se sincroniza" >> $LOG
                                rsync -avzuc --delete --stats --exclude=excludes-software.txt -e "ssh -i /root/.ssh/bcncorptosantacorp" /home/bcncorp/$DIR/ $USER@$HOST:/home/santacorp/$DIR/ >> $LOG
                                find /home/bcncorp/$DIR/ -type f -print0 | xargs -0 md5sum > /home/bcncorp/md5sum-logs/md5sum-$DIR-nuevo.log
                fi
                echo "" >> $LOG
fi

in the script, i think that if the first condition is true, show the first message, but not there any proccess with this name, should be go to the "else", right?, okay, sometimes work okay, but other times not work, i test if the proccess is running, and not are running but print the first message, please help me.

i'm using centos 5.7, 32 bits.

thank you, regards.

grail 04-16-2012 12:33 PM

Could you explain what you think the following line does:
Code:

ps -ef | grep -v grep | grep 'sync-software'

oscarenzo 04-16-2012 01:13 PM

Quote:

Originally Posted by grail (Post 4654262)
Could you explain what you think the following line does:
Code:

ps -ef | grep -v grep | grep 'sync-software'

thank you for you reply, i try check how many proccess with this name are running.

I replaced this line by: /bin/ps -ef | grep -c 'sync-software', when save this result on a var: then when i run the script print a bad result.

by example if i run /bin/ps -ef | grep -c 'sync-software', the result is 0
but if i save this result on a var, and then i try print when i run the script, print a bad result :S

This is the code:

#!/bin/bash
TET=`/bin/ps -ef | grep -c 'sync-software'`
echo "$TET"

although that the script is not running sometimes print 2, 3, 4, its rare :S

grail 04-16-2012 01:45 PM

My reason for the previous question is the greps are in the incorrect order as the grep -v is to remove the false positive of grep 'sync-software' being in the results.

Your second test and results would seem to make little sense unless the -c option is not the standard grep option as per below:
Code:

-c, --count
              Suppress normal output; instead print a count of matching lines for each input file.  With the -v, --invert-match option (see below), count non-matching lines.  (-c is specified by POSIX.)

I find it hard to believe / follow that you could get counts when 'sync-software' is not running. Is it maybe possible that other programs are using this string / command and so
whilst the actual command is not running it appears as though it is?

oscarenzo 04-16-2012 01:54 PM

Quote:

Originally Posted by grail (Post 4654316)
My reason for the previous question is the greps are in the incorrect order as the grep -v is to remove the false positive of grep 'sync-software' being in the results.

Your second test and results would seem to make little sense unless the -c option is not the standard grep option as per below:
Code:

-c, --count
              Suppress normal output; instead print a count of matching lines for each input file.  With the -v, --invert-match option (see below), count non-matching lines.  (-c is specified by POSIX.)

I find it hard to believe / follow that you could get counts when 'sync-software' is not running. Is it maybe possible that other programs are using this string / command and so
whilst the actual command is not running it appears as though it is?

thank you for your time, so, how can i do then a simple script, that check if are there any proccess running, the if are more than 1 print "more than 1", else
"not working"?

thank you, regards,

grail 04-16-2012 03:29 PM

What if you try using pgrep, like:
Code:

if (( $(pgrep -c sync-software) > 1 ))
then
    <your stuff here>
else
    <your other stuff>
fi


oscarenzo 04-16-2012 03:41 PM

hello again grail,

i try your code of the follow way:

Code:

if (( $(pgrep -c sync-software) > 0 )) > /dev/null;
        then
                echo "" >> $LOG
                echo "############################" >> $LOG
                echo "Resultado para: $DIR" >> $LOG
                date >> $LOG
else
echo "message"
fi

and recieve the follow error:
pgrep: invalid option -- c
Usage: pgrep [-flvx] [-d DELIM] [-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]
[-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] [PATTERN]
sync-software.sh: line 11: ((: > 0 : syntax error: operand expected (error token is "> 0 ")


All times are GMT -5. The time now is 08:07 PM.