Home > OS >  single line bash script for wget with retries
single line bash script for wget with retries

Time:11-11

I currently run two commands:

So it waits for 3 minutes and then calls api up

I would like to change that so it continuously checks every minute for up to ten minutes until wget returns 200.

so it should be the equivalent of this php function, but in bash. It is important to be a one liner (can be multiple statements separated by ;)

foreach(range(1,10) as $i) {
     sleep(60);    
     try {  
         Http::get('https://somedomain.com/api/up');
         break;
     } catch(Exception $e) {
         if($i>=10) throw $e;
     }
}

the two things where my bash knowledge fails me:

  • how to do the try catch or check for a 200 response code.
  • how to get that all into one line/statement

CodePudding user response:

Use a for loop with all the statements separated by ;

for i in {1..10}; do sleep 60; wget 'https://somedomain.com/api/up' && break; done
  •  Tags:  
  • bash
  • Related