0

I've have this script who run another one in the background, without waiting to it to finish.

My script.php:

$cmd = "nohup php script2.php > /dev/null 2>&1 &";
exec($cmd);

My script2.php:

sleep(10);
mail("me@mail.com","test","ok");

If I run it in commandline, it works fine: the call finish inmediately and I get a new mail in my inbox after 10 seconds.

But if I call to my script with http://myserver/script.php, I don't receive anything.

Notice that using:

$cmd = "php script2.php > /dev/null 2>&1";

works in both calling methods. So there's something wrong with the http call and the use of nohup.

I also tried passthru and shell_exec instead of exec with the same results.

Also tried this just in case, but it didn't work in any case.

Community
  • 1
  • 1
Gorka
  • 43
  • 7
  • instead of /dev/null, can you redirect the output to a temporary log file and see if there's any errors listed in the log file? Also check your /var/log/httpd/error_log & /var/log/httpd/access_log files for errors. – tim berspine Apr 08 '15 at 20:29
  • thnx Tim: this was the clue to find part of the problem... I was so focus on the script working or not that I did fake test on my dev server. – Gorka Apr 10 '15 at 13:05
  • The problem on my computer is this, but it's sthg with I can deal for now. http://stackoverflow.com/questions/29112446/nohup-doesnt-work-with-os-x-yosmite-get-error-cant-detach-from-console-no-s – Gorka Apr 10 '15 at 13:14
  • @Gorka Did you find a solution to make `nohup` work on HTTP calls with PHP? What was your solution? Thank you! – tonix Aug 11 '20 at 08:28

1 Answers1

0

Create a file on your server called phpinfo.php.

This file will only have one line of code:

<?php echo phpinfo() ?>

If you're uncertain, there is a full tutorial here: http://www.inmotionhosting.com/support/website/php/create-phpinfo-page-to-see-php-settings A lot of hosting companies may disable exec() and you may be unable to change your php.ini file.

If so, your best bet would be to start with the php mail function. http://php.net/manual/en/function.mail.php

Additional Information about php.ini: Your original statement that it works from the command line but not from a call through the browser makes, to me, the php.ini directives the most likely culprit. When PHP is executed through a webserver, the execution of any script is dictated by these directives. The best reference is the PHP documentation: http://php.net/manual/en/ini.core.php. If you run the phpinfo.php from a browser on your server, you're looking for a section "Core" and within that the disable_functions table row.

On my development server, there are a lot of functions that are disabled; including exec() and fork().

If you can edit your php.ini file, you can remove the exec function from the line

disable_functions = 

and restart your webserver; although I would discourage it. That function is disabled for valid security reasons.

BSimpson
  • 74
  • 4
  • I BSimpson... firstly, thnx but your answer. But if you read correctly my question, using as comand "php script2.php > /dev/null 2>&1" works properly in both cases... So, exec() works fine on my server and so it does mail function. – Gorka Apr 08 '15 at 13:57
  • Just to confirm: my local, dev and production servers have this value for my Php core directive 'disable_functions': no value – Gorka Apr 09 '15 at 11:03
  • Okay. From your phpinfo. Does the webserver(in my case Apache) environment have the PATH set to include the location of nohup. If not, set the full path in $cmd and see if it works? And try @tim berspine's suggestion – BSimpson Apr 09 '15 at 13:48
  • Also, since you ran shell_exec, did you echo the return back to the browser? Was it NULL? – BSimpson Apr 09 '15 at 13:52
  • Thnx for your help: unfortunately, as I need to run script2.php in the background, it will return nothing... so, as a result of this, I'll get allways NULL even if the script2.php works fine (TESTED). php.net/manual/es/function.shell-exec.php – Gorka Apr 10 '15 at 13:30
  • However, your help and @tim's one was crucial to find the problem on my computer and fix it to run my script on both dev and production servers, the important ones :) Thnx so much! – Gorka Apr 10 '15 at 13:32