shell script for looping

Please provide a bash script for following description

i have ldapsearch command, the script should execute my ldapsearch command and get into loop till i kill the shell script, it should run ldapsearch command every sec and produce a log file  when the ldapsearch commands fails.

I will setup a cron job to execute the bash script. The OS is Linux

ldapsearch -h abc -p 555 -D "cn=kaka,ou=ttt,ou=papa,o=company" -w "xyz" -b "ou=ttt,ou=papa,o=company" -s sub "(cn=abc)"

please provide a script
wasmanIT AnalystAsked:
Who is Participating?
 
woolmilkporcCommented:
for i in $(seq 1 60); do
RESULT=$(/path/to/ldapsearch -h abc -p 555 -D "cn=kaka,ou=ttt,ou=papa,o=company" -w "xyz" -b "ou=ttt,ou=papa,o=company" -s sub "(cn=abc)" 2>&1)
if [ $? -ne 0 ] ; then
   echo $(date) $RESULT > /path/to/errorlog
   mail -s "ldapsearch failed" abc@email.com < /path/to/errorlog
   exit
fi
sleep 1; done
0
 
edster9999Commented:

for (( ; ; ))
do
        ldapsearch -h abc -p 555 -D "cn=kaka,ou=ttt,ou=papa,o=company" -w "xyz" -b "ou=ttt,ou=papa,o=company" -s sub "(cn=abc)"
        sleep 1s
done

Open in new window

0
 
woolmilkporcCommented:
for i in $(seq 1 60); do
/path/to/ldapsearch ... >/dev/null 2>/path/to/errorlog
sleep 1; done

Schedule the above to run every minute:

* * * * * /path/to/script



wmp
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
edster9999Commented:
Whoops missed the bit about the 'fail' log.
Assuming ldapsearch outputs to the errorstream then something like :

 
for (( ; ; ))
do
        ldapsearch -h abc -p 555 -D "cn=kaka,ou=ttt,ou=papa,o=company" -w "xyz" -b "ou=ttt,ou=papa,o=company" -s sub "(cn=abc)" 2>error.log
        sleep 1s
done

Open in new window

0
 
wasmanIT AnalystAuthor Commented:
what is "2>" means  and also what is 2>&1 means, just trying to understand the shell scritps
0
 
woolmilkporcCommented:
2>

means "redirect the error output, if any ("stderr") to the file whose name follows the >"

2>&1 means "redirect stderr to the same target where the normal output ("stdout") goes to."
0
 
wasmanIT AnalystAuthor Commented:
perfect can you please add two more logic for me where when it fails it will put timestamp in error.log and also send email to abc@email.com
0
 
wasmanIT AnalystAuthor Commented:
Thank you woolmilkpro
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.