The following works well

[root]# killall httpd

[root]# service httpd start
Starting httpd:                                            [  OK  ]

[root]# service httpd status
httpd (pid  2987) is running...

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

[root]# killall httpd && service httpd start
Starting httpd: 

[root]# 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?

3 Answers

Syed Rakib Al Hasan On Best Solutions

After much conversation with everyone on this question, i thought i will bring all the bits & pieces together from the various commentaries and conversations that took place here and write an answer to summarize all of that in one place.

The --wait switch was important in this situation. (However, the better solution is stated later. Read on.)

killall httpd --wait && service httpd start

This ensured that killall completed killing all httpd services and ONLY THEN proceed to starting a new httpd service.

However, i also learned that, simply doing service httpd restart would be the better thing to do in this scenario than killing all httpd and starting a new httpd.

Helpful comments (to derive this answer) were made by:

Tachyon On

In this case I think you should use a semicolon instead of the double ampersand. I don't think the killall command always signals appropriately for what you're trying to do. Certainly if there are no running httpd tasks it will fail.

that other guy On

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.