Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

shell script for looping

Posted on 2011-10-14
8
Medium Priority
?
482 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:wasman
  • 3
  • 3
  • 2
8 Comments
 
LVL 20

Expert Comment

by:edster9999
ID: 36971108

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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36971117
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
 
LVL 20

Assisted Solution

by:edster9999
edster9999 earned 200 total points
ID: 36971120
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:wasman
ID: 36979652
what is "2>" means  and also what is 2>&1 means, just trying to understand the shell scritps
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36979705
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
 

Author Comment

by:wasman
ID: 36980004
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
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 1800 total points
ID: 36981377
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
 

Author Comment

by:wasman
ID: 36981532
Thank you woolmilkpro
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Fine Tune your automatic Updates for Ubuntu / Debian
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question