[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Run a background PHP process

Posted on 2011-05-08
7
Medium Priority
?
535 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:shishir_sri
7 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35716418
Is the whitespace here intentional (php tweet)?
php tweet_collector.php
0
 
LVL 3

Author Comment

by:shishir_sri
ID: 35716534
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
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35716540
You may want to try with the full path to the tweet_collector.php script.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:shishir_sri
ID: 35716618
I tried calling the script with the full path, and it still didn't work.

0
 
LVL 48

Expert Comment

by:hernst42
ID: 35717921
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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 35719903
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
 
LVL 3

Author Comment

by:shishir_sri
ID: 35725035
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 11 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question