Sam
Sam

Reputation: 4487

PHP Background Process Not Working

I'm using the following website to help me with background processes in PHP - http://nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/

My code is basically the same except my shell_exec method looks like this -
nohup php -f /my/path/to/import_products.php 2 > /dev/null 2> /dev/null & echo $!

The PHP script it's executing runs just fine and the products are imported, but when I add a sleep(10) to import_products.php the while(is_process_running($ps)) only loops once and exits.

It seems like the command shell_exec is executing is completing immediately instead of waiting for 10 seconds (from the sleep(10)).

Any ideas why this might be?

Edit
I guess my question really is "Why isn't the process waiting for the PHP script to exit before it stops showing up in ps?" - Surely as the PHP script is sleeping for 10 seconds ps $pid should show that process for a minimum of 10 seconds?

Upvotes: 2

Views: 1344

Answers (1)

Dev
Dev

Reputation: 448

Please modify your code accordingly.

  1. Add #!/usr/bin/env /usr/bin/php at the top of your php file before starting of php tag.
  2. Change shell_exec("nohup /usr/bin/php /my/path/to/import_products.php > /dev/null &").

Please note my php path is /usr/bin/php and I am using ubuntu os.So if your php executable path is different then replace this accordingly.

Upvotes: 1

Related Questions