3

I know it is possible to create a background PHP process which can be started on demand from another PHP file:

$command = "/usr/bin/php5 -f script.php";
exec( "$command > /dev/null 2>&1 &", $arrOutput);

However this solution works only when PHP is running as mod_php.

Is there any way to do the same on FastCGI?

It seems that on FastCGI the process is started and closed again and again by FastCGI, anyone has experience fixing it?

mgo
  • 174
  • 6
  • Yes, you can do this. Your permissions just need to be set correctly. – Brad Oct 15 '12 at 17:54
  • Yes, but I tried and it does not work. The process seems to start, but it does not do anything. It even does not end after the time it should (sleep(30));. – mgo Oct 15 '12 at 19:02
  • i too am having problems with fastcgi and exec since updating plesk from 9 to 10. It doesn't seem to call the function. in this case convert or ffmpeg. Did you find a solution? – v3nt Feb 17 '13 at 13:05
  • Same here executing `convert` ... still no solution and I do not want to go back to `mod_php` as it is way too slow for my needs – Fabrizio Feb 18 '16 at 23:58

1 Answers1

0

You could setup a cron job? If not, and it must be called within PHP i have once emulated it in a cross platform way without having access to exec() etc by using curl (believe it or not).

Create the .php script and make it public, and add this to the top:

if (isset($_POST['key']) == false || $_POST['key'] != 'your secret key') {
    die(); //request not allowed
}

then from the other PHP file, create a secure (https) curl connection and POST the secret key to it, set a timeout on curl of say 5 seconds (you can also send a http close connection header from the request page) so that the calling script wont freeze if the request takes to long to complete, in your case 30 seconds.

This will do the follow: 1. curl will access the page securely (stops just anyway accessing it in their browser) 2. curl will wait 5 seconds, then close the connection (but BOTH your php scripts will carry on)

It's also compatible regardless of OS internals, shells, etc. You can also tweak the timeouts etc as u see fit.

Not a brilliant solution but hope it works well enough for you.

HenchHacker
  • 1,616
  • 1
  • 10
  • 16