0

I've got some PHP code that I want to run as a background process. That code checks a database to see if it should do anything, and either does it or sleeps for awhile before checking again. When it does something, it prints some stuff to stdout, so, when I run the code from the command line, I typically redirect the output of the PHP process to a file in the obvious way: php code.php > code.log &.

The code itself works fine when it's run from the shell; I'm now trying to get it to run when launched from a web process -- I have a page that determines if the PHP process is running, and lets me start or stop it, depending. I can get the process started through something like:

$the_command = "/bin/php code.php > /tmp/code.out &";
$the_result = exec($the_command, $output, $retval);

but (and here's the problem!) the output file-- /tmp/code.out -- isn't getting created. I've tried all the variants of exec, shell_exec, and system, and none of them will create the file. (For now, I'm putting the file into /tmp to avoid ownership/permission problems, btw.) Am I missing something? Will redirection just not work in this case?

2 Answers 2

1

Seems like permission issues. One way to resolve this would be to:

  1. rename your echo($data) statements to a function like fecho($data)
  2. create a function fecho() like so

.

function fecho($data)
{
  $fp = fopen('/tmp/code.out', 'a+');
  fwrite($fp, $data);
  fclose($fp);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was thinking about that; it would probably work, but I'd really rather have the output going to stdout -- it makes the debugging/testing a lot easier when I'm working on those bits. In any case, /tmp is wide open as usual, so I don't see where the permission problems would be coming from. Still puzzled...
0

Blurgh. After a day's hacking, this issue is finally resolved:

  • The scheme I originally proposed (exec of a statement with redirection) works fine...
  • ...EXCEPT it refuses to work in /tmp. I created another directory outside of the server's webspace and opened it up to apache, and everything works.

Why this is, I have no idea. But a few notes for future visitors:

  • I'm running a quite vanilla Fedora 17, Apache 2.2.23, and PHP 5.4.13.
  • There's nothing unusual about my /tmp configuration, as far as I know (translation: I've never modified whatever got set up with the basic OS installation).
  • My /tmp has a large number of directories of the form /tmp/systemd-private-Pf0qG9/, where the latter part is a different set of random characters. I found a few obsolete versions of my log files in a couple of those directories. I presume that this is some sort of Fedora-ism file system juju that I will confess to not understanding, and that these are orphaned files left over from some of my process hacking/killing.
  • exec(), shell_exec(), system(), and passthru() all seemed to work, once I got over the hump.

Bottom line: What should have worked does in fact work, as long as you do it in the right place. I will now excuse myself to take care of a large bottle of wine that has my name on it, and think about how my day might otherwise have been spent...

1 Comment

Congrats for getting it sorted out. And enjoy your wine!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.