0

I have to execute a php script (a.php) in the background. I tried this but it's not working:

<?
$cmd = "php /home/megad404/www/prove/a.php &> /dev/null &";
exec('/bin/bash -c "'.$cmd.'"',$output,$return);
if($return===0)
{
    echo 'Successful';
} 
else
{
    echo 'Unsuccessful';
}
?>

It returns "Successful" but it doesn't execute a.php

a.php:

<?
file_put_contents(date("s"),"");
sleep(5);
file_put_contents(date("s"),"");
sleep(5);
file_put_contents(date("s"),"");
?>

a.php writes a file every 5 second and it works fine, except if I try to execute it in the background with the first script.

5
  • php shell scripts still MUST have <?php in them to trigger php mode. otherwise php will just treat it as text. remember: there's no such thing as a "php script". There's just files which have php code blocks in them. Commented Apr 16, 2013 at 21:26
  • You are getting "successful" every time since you bash is returning successfully - nothing to do with you php perhaps. Commented Apr 16, 2013 at 21:27
  • I know, but what should I do to make it works? Commented Apr 16, 2013 at 21:30
  • 1
    According to the question stackoverflow.com/questions/4646788/… you don't need to use bash -c. Does it work if you do it as shown there? Commented Apr 16, 2013 at 21:33
  • Drop /bin/bash -c and then run it. You might need the full path to php. which php will get it for you. Commented Apr 16, 2013 at 21:36

3 Answers 3

1

You can try adapt mi script. Look a command shell_exec() not exec(). First return all , second only last line.

function run_in_background($Command, $Priority = 0) {
         if($Priority)
             $PID = shell_exec("nohup nice -n $Priority $Command > /dev/null & echo $!");
         else
             $PID = shell_exec("nohup $Command > /dev/null & echo $!");
         return($PID);
 }
     //Verifies if a process is running in linux
function is_process_running($PID) {
         exec("ps $PID", $ProcessState);
         return(count($ProcessState) >= 2);
}

and example

$PIDPHP=run_in_background("php -S 127.0.0.1:18086 ".__DIR__."/index.php"); // or any other process.

if (is_process_running($PIDPHP)){
    exec("kill $PIDPHP");
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could also look into using real Posix/PCNTL functionality to actually detach the script to background, eg. with pcntl_exec and pcntl_fork(). This is after my opinion the right way to handle background scripts that runs for a longer period of time as you can communicate with the child/process to get updates, status and so on and even have them understand real signal handling.

PCNTL - http://www.php.net/manual/en/book.pcntl.php

POSIX - http://www.php.net/manual/en/book.posix.php

Cheers

Comments

0

This just worked for me:

<?php

$cmd = "/usr/bin/php /home/auser/a.php &> /dev/null &";
exec($cmd,$output,$return);
sleep(30);
if($return===0)
{
    echo 'Successful';
} 
else
{
    echo 'Unsuccessful';
}
?>

I saved it as runa.php and ran it from the command window as php runa.php. It produced 3 files.

running a.php also worked from the cron job:

]$ crontab -l
18 * * * * /usr/bin/php /home/auser/a.php

I put the script in a web directory and find that I have some writing problems. What can you see in the server log?

sudo tail -f /var/log/httpd/error_log

And what if you hit a.php from the web browser? Because you mention the script is 755, but how about the directory. Maybe it needs to be 775 or 777 for testing so that the script can write a file?

For testing I created a sub directory "output" and changed a.php

<?php
ini_set('date.timezone','America/New_York'); //without this it makes extra messages

error_log("a.php putting contents", 0);
file_put_contents("output/".date("s"),"");
sleep(5);
file_put_contents("output/".date("s"),"");
sleep(5);
file_put_contents("output/".date("s"),"");
error_log("a.php done", 0);
?>

It was unable to write files until I gave write permission to the ouput folder

sudo chmod 777 /var/www/html/output

Then I found out the apache user is writing the files:

~]$ sudo ls -l /var/www/html/output/
total 0
-rw-r--r--. 1 apache apache 0 Apr 18 11:38 00
-rw-r--r--. 1 apache apache 0 Apr 18 11:38 05
-rw-r--r--. 1 apache apache 0 Apr 18 11:37 55

So I changed the owner of output, in order to tone down the permissiosn again.

~]$ sudo ls -lu /var/www/html/ | grep output
drwxr-xr-x. 2 apache root 4096 Apr 18 12:21 output

This also works now:

 ~]$ sudo ls -l /var/www/html/output
total 0
-rw-r--r--. 1 apache apache 0 Apr 18 12:21 44
-rw-r--r--. 1 apache apache 0 Apr 18 12:21 49
-rw-r--r--. 1 apache apache 0 Apr 18 11:37 55

10 Comments

nothing changed... it returned Successful but nothing happened
I ran the changes above and got results both ways.Maybe sleep was enought to give the other script time to run. I don't know if you can have it launch and run in the background though from the browser. The cron is better.
It doesn't work... Could it be a server issue? Is there another way to run a script in the background?
Hey. what about your user write permissions? Try su -c "/usr/bin/php /path/to/a.php"
Oh, the one server issue could be your short tag, as another commenter noted. Use the <?php in your "a.php" Some times I saw files fail if the server had the "no short tags" option set.
|

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.