0

I am writing a shell script and here is the snippet ..

sudo service httpd restart   --- ( 1)  

if [ $? -eq 0 ]; then

     # Now configure php.ini using sed command 
     sudo sed -i 's_;date.timezone =_date.timezone = "Asia/Kolkata"_' /etc/php.ini 

  # Now restart the httpd server again 

  sudo service httpd restart  ---- This statement throws error 

When I ran above script then I got error Address already in use: make_sock: could not bind to address at second sudo service httpd restart statement.

I doubt it is because when first time sudo service httpd restart runs , before it finishes completely second 'sudo service httpd restart' runs.

So how can I test surely If first sudo service httpd restart finishes then only rest of the code execute.

I hope I am understandable ..

Thanks

2 Answers 2

3

Forget the [ giving:

if sudo service http restart; then
    # stuff
    if sudo service http restart; then
        echo ok
    else
        # error handling for second service failure
    fi
else
    # error handling for first failure
fi

Commands are true if their return status is zero, false otherwise. Shell commands also must terminate before the next line is called.

It is possible, in principle, that service does stuff in the background and exits before completion. In practice, it doesn't because that would really screw things up.

Sign up to request clarification or add additional context in comments.

Comments

0

Not sure if the command backgrounds. If so, you could check if your command is still to be found in the process list:

// ... your code ...
c=1
while [[ c -gt 0 ]] 
do
    c=`ps l | grep -c "service httpd restart"`
done
service httpd restart 

2 Comments

if service command is not in backgroud then will if [] statement will return 0 only when httpd restart completely ?
No, this way it will only check if the command is still running. If it finished (either way) the while loop will end and the next restart will happen. @msw surely has posted the better solution if you need to handle errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.