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

The problem here is that sometimes the stop command takes longer than 5 seconds, which will cause the start to fail with a "server already running" message. After the start fails, the stop will complete, which leaves the server down. Hence looking for a better solution to make sure that server is fully stopped before the start is excecuted.

Ashley
  • 1,447
  • 3
  • 26
  • 52
  • 1
    `sudo ./apachectl start; while [ $? -ne 0 ]; do sudo ./apachectl start; sleep 1; done` – bishop Aug 23 '16 at 15:25
  • you mean after stop i should use that sleep 5 and then do a start, then use while [ $? -ne 0 ] then next line start and finally sleep 1....right? – Ashley Aug 23 '16 at 15:42
  • it means it will try to start the server as long as the exit status from the start script is not 0. I would add a sleep in the middle, otherwise it may try to run it many many times :) – Daniel Ferradal Aug 23 '16 at 15:47
  • Thanks for the solution. I will definitely try this, but i would request you to kindly post this script in the formatted text so that i can exactly use that. I would admit i'm a novice in shell script and would not try something directly onto my server. I can't figure out where in middle you want to use that sleep.:) – Ashley Aug 23 '16 at 15:52
  • either I didn't see it or bishop already added it, so in any case, nevermind. Use the whole line bishop pasted ;) – Daniel Ferradal Aug 23 '16 at 16:30
  • I would like to restrict the option to start the server only with 5 attempts, so how do include that condition alongside existing while loop. I mean something like var=1, while [ $? -ne 0 -o $var=5] and within do an increment statement like var=$((var+1)), looking for exact code, since i'm not good at shell syntax, though i know the logic – Ashley Aug 30 '16 at 20:17

1 Answers1

1

I'd suggest apachectl restart if you're not doing anything else between stop & start.

covener
  • 17,402
  • 2
  • 31
  • 45