4

I'm trying to trigger a PHP script to run in the background using the exec() function but I cannot get it to work. I've read countless posts on stack overflow and other forums and tried many variations to no avail.

Server Info:

Operating System: Linux
PHP: 5.2.17 
Apache Version: 2.2.23
Home Directory: /home1/username

I'm currently using the code:

exec("/home1/username/php /home1/username/public_html/myscript.php > /dev/null &");

When I run the above script I get no error_log and no error in my cPanel error log, however the script definitely doesn't execute. When I browse to http://www.mydomain.com/myscript.php it runs and e-mails me instantly. Any idea why this isn't working / how I can find out what error is being produced?

Update cPanel Process Manager Output

exec("php /home1/username/php /home1/username/public_html/myscript.php > /dev/null &");

Produces:

27183   php /home1/username/php /home1/username/public_html/myscript.php
27221   [sh]
27207   php /home1/username/php /home1/username/public_html/myscript.php
27219   php /home1/username/php /home1/username/public_html/myscript.php
27222   php /home1/username/php /home1/username/public_html/myscript.php
27224   php /home1/username/php /home1/username/public_html/myscript.php
27249   sh -c php /home1/username/php /home1/username/public_html/myscript.php > /dev/null &

Is that normal? Script appears to hang around for a long time even though it should execute very quickly.

4
  • How are you running the exec()? From a shell script, another PHP file? Is your PHP really installed at /home/username/php? Commented Sep 23, 2012 at 2:56
  • I'm running it from another php script. Yeah in my FTP is shows as /php and my home dir is /home1/username. Any way to verify this? Commented Sep 23, 2012 at 3:27
  • I'm guessing you're running this under Apache? Does Apache have read+execute writes on the script and all the directories leading up to it? Commented Sep 23, 2012 at 3:36
  • Yes it does have the permissions to do so. Apache Version: 2.2.23 Commented Sep 23, 2012 at 3:40

3 Answers 3

4

Couldn't get the exec working with php. Even when I got shell access to the server the command just hung. I decided to use wget instead which accomplishes the same thing. Works great :)

exec("wget http://www.mydomain.com/myscript.php > /dev/null &");
Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried invoking the php CLI directly?

exec("php /home1/username/php /home1/username/public_html/myscript.php > /dev/null &");

You will not need the #!, which would output to the browser if called through Apache.

EDIT. It looks like your script is working, but your PHP script executing in the background is hanging (not exiting). Try this variation:

exec("php /home1/username/php /home1/username/public_html/myscript.php > /dev/null 2>&1 &");

What does “> /dev/null 2>&1″ mean?

13 Comments

when I ran that I just got a white page, no error no script run. Is there a way I can check for errors somehow? error_log is not being generated
maybe instead of /dev/null I can change it to see the output somehow?
Can you run that command directly on the command line? It sounds like you have a crash (blank page) and your error logging must be set too low. Is exec() even allowed by your host?
Sure, try sending to output to a file you can tail.
Thanks, tried that but no luck :/ I'm baffled by this one..I'll try asking BlueHost if they have any suggestions either.
|
0

since you want to run the myscript from your command line, wy not do this:

exec('(/home1/username/public_html/myscript.php) > /dev/null &',$r,$s);

And write this as a first line in the myscript.php:

#!/home1/username/php -n
<?php
    //script goes here
?>

That should work. The hashbang tells the system what programme to use to run the script that follows, so you don't need to add that to your exec call. Also, it's safer (and therefore better) to put brackets around the full script call, just so PHP knows what output has to be redirected to what stream, to avoid any issues that might occur. Especially when libs or packages like PHP-GTK are installed on the server (hence the -n option).

6 Comments

I tried that but it's the same issue. Doesn't run my script and doesn't output any error. Just a blank white page with no error_log file
@user1647347: What do you see when you add var_dump($r); and echo $s; just after the exec statement? if $s !== 0, there's a problem with the script you're calling... try it without redirecting the output to /dev/null first, bc you're effectively hiding the output with your code.
without the /dev/null-bit? are you sure that the php.ini is configured correctly, so that mail works as it should? By showing output I mean: do a regular exec call: (exec('/path/to/script.php',$r,$s);, and then echo the $r and $s vaLs
Yes - when I run the script directly it emails me instantly.
When you run it from the exec call? In which case, there's no issue, is there?
|

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.