#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    50

    Exec() to launch php as background process - not working


    I am struggling with getting a php file to run in the background with PHP's exec().

    Following the advice here:

    http://www.developertutorials.com/tutorials/php/running-background-processes-in-php-349/

    and here:

    http://forums.devshed.com/php-development-5/how-to-make-phpmailer-email-in-the-background-613073.html

    As a first test, I try
    exec("ls -l > logfile.txt 2> errfile.txt &");
    That works fine. logfile.txt gets filled with a directory listing.

    Per instructions in the php documentation, since the exec kicks off a process that runs in the background, standard out (and standard error) are redirected to a file.

    Now, I try
    exec("/usr/bin/php -f /path/to/my.php > logfile.txt 2> errorfile.txt &");

    It appears nothing happens.

    Here are test files that I'm trying:

    alpha.php
    PHP Code:
    <?php
      $version
    ="a";
      
    // Go do something we do not need to wait for.
      
    exec("/usr/bin/php -f /path/to/beta.php > logfile.txt 2> errorfile.txt &");
    ?>
    <html>
    <head><title>Test</title></head>
    <body>
    <p>This is Alpha version <?php echo $version?></p>
    </body>
    </html>
    beta.php
    PHP Code:
    <?php
    if (!($fp fopen('/home/johnst12/public_html/workshops/admin/betadata.txt''w'))) { exit;}
    fprintf($fp"Proof that Beta executed.");
    fclose($fp);
    ?>
    If I run beta.php directly, it works fine. Betadata.txt gets the message.

    If I run alpha.php to launch beta.php, betadata.txt is not created. logfile.txt and errorfile.txt remain empty (expected).

    I am sure that the path to php, and the path to my php file are correct.

    Googling for clarification has not been fruitful. A couple of common themes seem to be (a) running out of resources? (b) lack of permission on the target php file? Out of resources seems unlikely. The permission on the script is global read 644 (rw-r--r--). I tried adding execute (755) just in case it would help. It made no difference.

    PHP version 5.3.21
    Linux/Apache system.
    safe_mode Off

    What am I missing?

    Thanks.

    P.S. I tried /usr/bin/php -f /.../myfile.php both with and without the "-f" parameter. The man page said to use "-f". The examples did not. Neither method made a difference. No action is taken by the called php file.
    Last edited by EEsterling; April 26th, 2013 at 10:18 PM.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,068
    Rep Power
    9398
    php doesn't like running like that. Something to do with stdin. Try with nohup:
    PHP Code:
    exec("nohup /usr/bin/php -f /path/to/beta.php > logfile.txt 2> errorfile.txt &"); 

    With -f anything else that looks like a flag will go to PHP, so if you wanted to pass a "-x" option to your script then you'd have to
    Code:
    /usr/bin/php -f /path/to/beta.php -- -x
    Without, options before the filename go to PHP and after go to the script.
    Code:
    /usr/bin/php /path/to/beta.php -x
    Since you don't have arguments to the script, the difference doesn't matter.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    50
    Thanks. I added the nohup, but no joy. beta.php still appears to not run.

    One problem is I can't be sure if it is not running, or failing on the file open and exiting. But I can imagine no reason it would fail. (It works when I run beta.php by itself.)

    The file open/write/close was the simplest thing I could think of to do to test that the file is doing something as a background process.


    Also, I checked my error log (Apache log) nothing there.
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,068
    Rep Power
    9398
    I assume you've already looked at the two files in case they have output or errors?

    A few other things to check:

    Delete the two files. Are they recreated each time this code runs?

    PHP Code:
    exec("nohup /usr/bin/php -v > logfile.txt &"); 
    should output version information to that log file.

    PHP Code:
    exec("/usr/bin/php -f /path/to/beta.php > logfile.txt 2> errorfile.txt"); 
    should run the script properly (but not in the background).
  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Have you verified that /usr/bin/php is the correct path to PHP?
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    50
    I think I've got it.

    Originally Posted by requinix
    Delete the two files. Are they recreated each time this code runs?
    Yes. I was doing that. And the files were being created each time. With the straight beta.php producing no output they were 0 bytes.

    Originally Posted by requinix
    output version information to that log file (with -v)
    This and E-Oreo double questioning me I think led to the answer.

    Originally Posted by E-Oreo
    Have you verified that /usr/bin/php is the correct path to PHP?
    I did mention in the first post, I confirmed that. But I confirmed that there was a real executable there...

    Code:
    > ls -l /usr/bin/php
    -rwxr-xr-x 1 root root 27172203 Feb 14 23:55 /usr/bin/php
    But last night I also tried ls -l /usr/bin/php* to see what else was in there.

    There is a /usr/bin/php-cli which is a link to /usr/local/bin/php.
    Code:
    lrwxrwxrwx 1 root root       18 Feb 14 23:56 /usr/bin/php-cli -> /usr/local/bin/php
    So, last night I tried changing to /usr/local/bin/php to see if it made any difference. It didn't appear to. (But I now think I may have not been patient enough for the file to update?)

    This morning, I tried:
    PHP Code:
    exec ("nohup /usr/bin/php -v > logfile.txt"); 
    That appeared to hang. So, I tried:

    PHP Code:
    exec ("nohup /usr/local/bin/php -v > logfile.txt"); 
    That successfully sent the version info to logfile.txt.

    So, I went back again to my test to make sure that alpha.php was using /usr/local/bin/php and that everything else looked right. Ran it again. My test seems to be working fine now. (Again, thought I had already done so, but guess I didn't wait long enough.)

    So, the moral for future readers struggling with this appears to be: do not just make sure your "path to php is right" but make sure you are using the "path to the right php which actually supports the command line interface."

    requinix's suggestion of php -v appears to be a simple test that you are using the correct php executable.

    Thanks to both.
  12. #7
  13. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,068
    Rep Power
    9398
    So it works now? Because I was going to say that the fact that /usr/bin/php is a symlink is not only common but probably correct. You should be using that, not whatever the link points to.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    50
    /usr/bin/php is not a link. Check the post again. But yes it works using /usr/local/bin/php.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0
    You might also take a look at forking processes if you want "real" background processing : php . net / manual/en/function.pcntl-fork.php

IMN logo majestic logo threadwatch logo seochat tools logo