bashchainingampersand

Chaining Bash command with double ampersand - unexpected behavior


The following works well

> killall httpd

> service httpd start
Starting httpd:                 [  OK  ]

> service httpd status
httpd (pid  2987) is running...

The following is supposed to work like above, but it's not

> killall httpd && service httpd start
Starting httpd:

> service httpd status
httpd is stopped

Note that here it didn't return the [OK] signal for starting httpd.

What is going wrong? Do Bash commands by ampersand chaining run asynchronously? How can I make sure that they run synchronously one after another?


Solution

  • The problem is that signalling (with killall) is asynchronous.

    In your non-interactive case, the server is still running when you ask it to start, because it hasn't had time to exit.

    In your interactive case, the second long delay before you issue the next command is enough to allow the process to shut down. Starting it again therefore works.

    The proper solution is service httpd restart, which both kills and starts the server.