0

I have a php scrip, in which i have written the following code

$client = new \Predis\Client();
$client->select(4);
$client->lpush('emailid',$x['to']);


$command = "/usr/bin/php5 -f /var/www/Symfony/src/Ens/NewBundle/Controller/cron.php";

exec( "$command > /dev/null &", $arrOutput );

return $this->render('EnsNewBundle:Email:header.html.twig');

in this I have written an another php script named as cron.php. I want to run that script in background. and I want to check that is this running in background or not. how can i check that

j0k
  • 22,600
  • 28
  • 79
  • 90
Rohitashv Singhal
  • 4,517
  • 13
  • 57
  • 105
  • 1
    possible duplicate of [php execute a background process](http://stackoverflow.com/questions/45953/php-execute-a-background-process) – DhruvPathak Aug 23 '12 at 06:09

2 Answers2

5

Maybe you could have a look to the Symfony2 Process component.

It's quite useful for running command from PHP.

Alexander
  • 23,432
  • 11
  • 63
  • 73
user1041440
  • 191
  • 1
  • 7
1

You can take the output of cron in a file by > filename and check if it really runs.

Or check in process list if there is a new php process stating when you run this one.

You should also look at Codememe bundle here

Do check open source queuing systems too, they are helpful many times. Like Beanstalkd or RabbitMQ

You can push data to these queues, they can be say "filenames" and other worker takes data from the "tubes" of queues and apply say "php filename" and then picks up next data from queue.

amitchhajer
  • 12,492
  • 6
  • 40
  • 53
  • when i m running on command line `php test.php` then it is giving correct answer but when i am running this command in another php file as follow: `echo shell_exec('php test.php | /dev/null &');` then it is giving no answer – Rohitashv Singhal Aug 23 '12 at 06:40
  • when you do shell_exec the process starts in background, so take some standard output from test.php to a file using `shell_exec('php test.php > file');` , you don't need '&' here. And check if test.php returned successfully using echoing something at file termination, which will be stored in file – amitchhajer Aug 23 '12 at 06:46
  • this is not giving any response – Rohitashv Singhal Aug 23 '12 at 07:51
  • when i am running whoami directly in this exec() then its giving correct ans but when i am execution this in test,php and executing test,php in index.php then it is giving blank – Rohitashv Singhal Aug 23 '12 at 07:53
  • is test.php made to do anything? try something like sexec("mkdir dir") in test.php and check whether it is running or not. – amitchhajer Aug 23 '12 at 07:58
  • this is my test.php `$output=shell_exec('whoami'); echo $output; ` – Rohitashv Singhal Aug 23 '12 at 08:10