How to keep process running after apache restart.

Hi, I have a PHP application that starts a couple of processes on the server...the problem is that if I restart apache those running apps will die.

How can I start them in a way that they are not killed when I restart/stop apache ?

      $cmdstr = "nohup ".$config['hsp_path']."/".$config['sc_trans']." ".$config['hsp_path']."/autodjs/".$port."/".$srvname.".conf";
      $cmdstr .= " > /dev/null & echo $!";
      $pid = shell_exec($cmdstr);

This is how I start that app.

You should do this:

service httpd reload

'reload' option does not close alive connections.

Thank you for your reply but that is not the solution I am looking for, still waiting for suggestions.

I'm guessing that the parent process is sending a term signal to each child process. The nohup command will not be effective in this case as it only ignores the hangup signal; it may also not be effective if the invoking shell is not associated with a tty, but that's neither here nor there.

What might help would be to wrap your command in a shell subcommand that causes interrupt, term, hup and quit signals to be ignored. I've no experience with PHP, but from your example it might look something like this:

 $cmdstr = "ksh -c '(  trap \"\" 1 2 3 4 15;  "
$cmdstr .= ".$config['hsp_path']."/".$config['sc_trans']." ".$config['hsp_path']."/autodjs/".$port."/".$srvname.".conf";
      $cmdstr .= " > /dev/null  )' &;  echo $!";
      $pid = shell_exec($cmdstr);

The idea behind this is that the PHP causes kshell (probably works in bash too) to be invoked to execute the command. The command given to the shell begins with a trap command that causes signals 1,2,3,4 and 15 to be ignored (the "") before executing the intended command.

I've assumed that the .= PHP operator is a += like operator and appends the string to the current value.

For a simple example, if my assumptions about PHP are wrong, consider the following that invokes the command t24 using this technique.

ksh -c '( trap "" 1 2 3 4 15; t24 )' &

I specifically invoke ksh as some systems invoke sh with a 'shell-exec' command and I don't know if plain Bourn shell supports this or not. If ksh is not in your path you will need to use the fully qualified pathname.

Hope this helps.

Thank you for the reply, I have implemented your method but when apache is restarted the process still dies,

  $cmdstr  = "ksh -c '(  trap \"\" 1 2 3 4 15;  ";
  $cmdstr .= $config['hsp_path']."/".$config['sc_trans']." ".$config['hsp_path']."/autodjs/".$port."/".$srvname.".conf";
  $cmdstr .= " > /dev/null  )' & echo $!"." ;";
  $pid = shell_exec($cmdstr);      

One more slight tweek to try, then Im out of ideas:

$cmdstr  = "ksh -c '(  trap \"\" 1 2 3 4 15;  ";
  $cmdstr .= $config['hsp_path']."/".$config['sc_trans']." ".$config['hsp_path']."/autodjs/".$port."/".$srvname.".conf";
  $cmdstr .= " > /dev/null & )' & echo $!"." ;";
  $pid = shell_exec($cmdstr); 

Adding an amper (&) before the closing paren should cause the process to become 'detached' from its process group and parent process (ppid should be 1) and unavailable to be signaled as a part of the process group (assuming that this is how/why the process is terminating as apache is bounced). The side effect of this is that the pid assigned to $pid will be incorrect; it will be that of the subcommand that invoked the process rather than the process itself.

Good luck.

Thank you for your efforts, it's getting killed , is there no way of keeping a process alive that was started from php ? The process dies on reload/stop/etc...