Sample scripts are provided as-is with no warranty of fitness for a particular purpose. These scripts are solely intended to demonstrate techniques for accomplishing common tasks. Additional script logic and error-handling may need to be added to achieve the desired results in your specific environment.
limit_retries.s
Error-handling generally falls into to two categories:
- Unrecoverable errors, where you typically want to abort the process and notify somebody
- Recoverable errors, where you want to simply re-try the failed attempt until it works.
Things get a little trickier when you encounter what should be a recoverable error, but under certain circumstances does not recover. If you are not careful, you can end up with an infinite loop.
One way to avoid this is to use LOOPCOUNT and related commands to limit the number of times you retry something.
The following script attempts to connect to an FTP site 10 times, pausing for one minute between attempts. If it can't connect after 10 tries, it aborts and sends an email notification.
1 loopcount 10
2 :connect_loop
3 ftplogon "mysite"
4 LOOPIF goto pause_and_retry else goto connected
5 ;; Connection attempts failed 10 times
6 CREATEMAIL "Script Notifier" "noreply@mycompany.com" "Connection failed" "FTP server appears to be down" ""
7 sendmail "smtp.mycompany.com" "Sys Admin" "sysadmin@mycompany.com"
8 EXIT
9
10 :pause_and_retry
11 pause /for=60
12 goto connect_loop
13
14 :connected
15 ;; Connection worked -- continue script

