Run a background PHP process

Hey,

I wish to run a PHP script in the background. Its basically supposed to run forever, and perform a task.

I can trigger it from the browser, and it runs perfectly, so I know that there is no problem in the script itself. Can someone tell me how I can trigger it through a PHP script?

Here is what I've tried, but its not working:

 
$output = array();
		$returnVal = 0;
		exec("/usr/bin/php tweet_collector.php > /dev/null &", $output, $returnVal);
		$log->message('\'exec\' command executed to start tweet_collector');
		$log->message("Return Value: $returnVal. Output: " . implode('|', $output));

Open in new window


The output that I get in my log is:

"Return Value: 0. Output: "

Thanks,
Shishir S.
LVL 3
shishir_sriAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
You might try starting the process by using CURL POST method.  I use this when I want to decouple process A from process B.  By "decouple" I really mean decouple.  Process A starts process B and there is no further handshake between the two processes.  Process B runs independently and has no browser output, so if you want to receive messages from it, you might consider using the output buffers to capture the messages, then using the PHP mail command to send the messages to your email address.
0
 
Lukasz ChmielewskiCommented:
Is the whitespace here intentional (php tweet)?
php tweet_collector.php
0
 
shishir_sriAuthor Commented:
Hey,

Thanks for the response. Yes the whitespace is intentional. From what I understand of the command, the "/usr/bin/php" is basically a call to the PHP interpreter, and the "tweet_collector.php" is the argument we're passing to the interpreter saying that this is the script that is to be executed.

best,
Shishir
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Lukasz ChmielewskiCommented:
You may want to try with the full path to the tweet_collector.php script.
0
 
shishir_sriAuthor Commented:
I tried calling the script with the full path, and it still didn't work.

0
 
hernst42Commented:
As you use exec and don't redirect stdout the script will hang.

From http://www.php.net/manual/en/function.exec.php
If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.


So try:
exec("/usr/bin/php tweet_collector.php > /dev/null 2>&1 &");
0
 
shishir_sriAuthor Commented:
Hey,

Thank you all, for the replies.

@hernst42: I've already redirected the stdout stream in the code above. I redirected the stderr stream too, after reading your suggestion. In any case, its not the solution since the problem is not with the script hanging.

@Ray: I'll try out the CURL option later. For now, I've simply started the processes using SSH.

Thanks for taking the time.

best,
Shishir
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.