0

We do frequent deployments using udeploy and we have there a shell script to restart the apache http server as the last task. Script is simple:-

cd bin_path
sudo ./apachectl -k stop
sleep 5
sudo ./apachectl start
while [ $? -ne 0 ]
do 
    sudo ./apachectl start
    sleep 1
done

Now i would like to include an extra condition in this while loop that checks for a certain value of the counter variable, so that attempt to restart the server is restricted to only say 5 times. Now here is what i want.

var = 0
sudo ./apachectl start
while [ $? -ne 0 -o $var lte 5 ]
do
    var = $((var+1))
    sudo ./apachectl start
    sleep 1
done

But somehow i'm not an expert in shell script syntax. If someone can help me correct the script to achieve the desired solution.

euphoria83
  • 14,768
  • 17
  • 63
  • 73
Ashley
  • 1,447
  • 3
  • 26
  • 52

2 Answers2

2

You have several problems.

  1. Shell variable assignments have no spaces around =.
  2. Your while loop is testing the status of sleep, not sudo.
  3. You're using or when you should be using and to combine the conditions.
  4. Comparisons in test need a - prefix.

The correct script should be:

var=0
while ! sudo ./apachectl start && [ $var -le 5 ]
do
    var=$((var+1))
    sleep 1
done
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hi Barmar, i'm not an expert with shell syntax but just a general question how does while ! sudo ./apachectl start work, i mean not even within braces and also here we are not checking the exit status of start command, also i guess it should be -le instead of -lte, right – Ashley Aug 30 '16 at 21:11
  • `while command` tests the exit status of the command. Putting `!` before the command inverts the exit status. There's no need for braces, that's just a particular command that's used to test the values of strings and numbers. – Barmar Aug 30 '16 at 21:13
  • actually i don't want to execute the start 5 times, i just want to attempt max 5 times to start it, if its starts in 1st attempt thats good, but should not exceed a max of 5. Also to mention i think the && operatoror would force both conditions, so i guess here it should be ||, right so that while attempts to start the server only max 5 times, if it gets started in 2 attempts thats good – Ashley Aug 30 '16 at 21:32
  • The `!` operator has higher precedence than the `&&` operator, so it only negates the status of `sudo`. So I'm pretty sure this will do what you want. It will stop as soon as `apachectl` succeeds or you reach 5 tries. – Barmar Aug 30 '16 at 21:41
  • or i guess best thing would be var = 0 sudo ./apachectl start while [ $? -ne 0 -o $var lte 5 ] do sleep 1 var = $((var+1)) sudo ./apachectl start done – Ashley Aug 30 '16 at 21:44
  • The OP, though using `while` constructs, is actually trying to start at least once before the loop. I suggest use the `until` construct. – alvits Aug 30 '16 at 21:46
  • @Ashley - if you believe that's the best thing to do, you should put an answer yourself and describe why it's the best. – alvits Aug 30 '16 at 21:50
  • Thats fine i mean the attempt could be six no issues there, just wanted to check if it is ok otherwise, i would also try Barmar option once i have tried this one. – Ashley Aug 30 '16 at 21:51
  • @alvits There's no reason to start before the loop. The loop will run `apachectl start`, and if it succeeds it will end the loop. – Barmar Aug 31 '16 at 04:25
  • In your solution it's not necessary. I was commenting on general construct that the OP was asking for and the construct that he is proposing. – alvits Aug 31 '16 at 04:52
  • @Barmar:- Apologies for misunderstanding your script. The script is syntactically and logically correct and i tested it with different commands not exactly apachectl start, just to make sure how it works,and pretty honestly i feel based on my requirements you tried best to provide a script based solution. But actually i have a different problem related to my server restart and i don't know how to address that or how to bring in to test that condition via script because that requires server scripting knowledge and i'm not an expert there.Well thanks for your effort:) – Ashley Sep 01 '16 at 15:10
0

"Fixing" the script seems like a futile exercise when the one-liner "apachectl restart" accomplishes the same thing.

covener
  • 17,402
  • 2
  • 31
  • 45
  • Add to that, when apache fails to start due to botched deployment, does it even make sense to re-try 5 times? – alvits Aug 30 '16 at 21:51
  • I mean sometimes it takes little long to stop, so just need to keep trying to start it with some number of attempts but definitely not for an infinite period of time. – Ashley Aug 30 '16 at 21:55