5

I have a script startbg.php:

echo `/usr/bin/php $dir/runbg.php >> $dir/logbg.txt 2>&1 &`;

Which I call from the web (via HTTP/Apache).

It runs runbg.php in a background process.

But if I restart Apache (/etc/init.d/apache2 restart), the background process is killed.

Is there anyway I can keep the process running in the background?

Petah
  • 45,477
  • 28
  • 157
  • 213
  • @RenePot I need to restart Apache to make configuration changes. – Petah Aug 22 '12 at 10:10
  • Have you tried running it from [command line](http://www.php.net/manual/en/features.commandline.usage.php)? – Gareth Parker Aug 22 '12 at 09:58
  • The script runs fine from the command line. It also runs fine from `exec` in PHP. The problem is that it stops when Apache is restarted if it was executed from a requested page. – Petah Aug 22 '12 at 10:12
  • the problem is that when pstree, the background process isn't listed as a child of apache2 ..., I am having troubles to find evidence of the relation between these 2 processes – Julien Apr 09 '15 at 11:00

3 Answers3

1

You could open up an instance of PHP's internal webserver (As of PHP 5.4.0, the CLI SAPI provides a built-in web server.) in my test killing httpd.exe did not effect php.exe:8000

<?php 
//Tested with windows

chdir('../php');
//S = Server, listen interface 0.0.0.0 = all : port 8000
//t = Served document root
echo `php -S 0.0.0.0:8000 -t C:\\xampp\\htdocs >> C:\\xampp\\htdocs\\logbg.txt 2>&1 &`;
?>

So possibly (untested):

echo `/usr/bin/php -S 0.0.0.0:8000 -t /srv/www/yoursite.com/public_html >> /srv/www/yoursite.com/public_html/logbg.txt 2>&1 &$dir/runbg.php >> $dir/logbg.txt 2>&1 &`;

Tho id have no idea how to kill it :s

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • Seems hacky. And I'm running PHP 5.3, nice try though :) – Petah Aug 22 '12 at 10:42
  • IMHO using php -S which is for develpment purpose for such a thing is not remmanded. A real politic of administration of the Apache should be taken to avoid to kill the process that are still running. On a production environment I manage I cant restart anything until everything is finished. Ortherwise we warn the users and plan to do a maintenance. My 2 coins ;) –  Aug 22 '12 at 11:12
  • @FoxMaSk yeah i agree. id be interested to know if the OPS question is possible to still use apache after killing it. – Lawrence Cherone Aug 22 '12 at 11:15
  • @LawrenceCherone what do you mean still use Apache after killing it? – Petah Aug 22 '12 at 21:39
0

You should change process group id of startbg.php or runbg.php.

Apache/PHP sends signals to all children of its process group. I cannot find any document on the feature. However, I resolved the issue with calling setpgid() from spawned process.

Chul-Woong Yang
  • 1,223
  • 10
  • 17
0

One way to allow the process to continue running without apache is to make it a service. Then activate the service (service runbg start) with your command. If you don't make the service normally running, then it will depend on your command to start after a reboot, but will not die with apache restarting.

TheSatinKnight
  • 696
  • 7
  • 16