LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-16-2012, 12:14 PM   #1
oscarenzo
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Rep: Reputation: Disabled
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.

Last edited by oscarenzo; 04-16-2012 at 01:39 PM.
 
Old 04-16-2012, 12:33 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,498

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Could you explain what you think the following line does:
Code:
ps -ef | grep -v grep | grep 'sync-software'
 
Old 04-16-2012, 01:13 PM   #3
oscarenzo
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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

Last edited by oscarenzo; 04-16-2012 at 01:31 PM.
 
Old 04-16-2012, 01:45 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,498

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
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?
 
Old 04-16-2012, 01:54 PM   #5
oscarenzo
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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,
 
Old 04-16-2012, 03:29 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,498

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
What if you try using pgrep, like:
Code:
if (( $(pgrep -c sync-software) > 1 ))
then
    <your stuff here>
else
    <your other stuff>
fi
 
Old 04-16-2012, 03:41 PM   #7
oscarenzo
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Unhappy

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 ")
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Commands work if executed one by one in bash terminal but not from script??? taylorkh Linux - General 6 11-12-2011 07:24 AM
BASH script: can't get a {cat > /dev/...} to work in background 2lunious Programming 16 10-03-2009 06:22 AM
work on memory with bash script chesterman Linux - General 1 05-04-2009 12:25 PM
BASH script does not work. Do not understand why. SteveInTallyFL Linux - Newbie 7 12-01-2008 09:32 AM
bash script doesnt work in slackware duBeN Linux - Software 1 08-30-2006 11:56 PM


All times are GMT -5. The time now is 12:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration