Shell_exec(): How to keep process active when restarting apache?

Asked

Viewed 225 times

2

The processes run by php shell_exec() end up restarting apache. It is possible to keep processes active without interruptions when restarting apache?

Below the code I’m using:

$Command = "ksh -c '(  trap \"\" 1 2 3 4 15; ";
$Command .= "./sc_serv /caminho-do-conf/sc_serv.conf";
$Command .= " > /dev/null )' & echo $!"." ;";

$ExecCommand = $Command;

echo "Comando Executado: " . $ExecCommand ;

$Result = shell_exec($ExecCommand);
echo "<pre>$Result</pre>";

2 answers

1

I believe that there is no way, because the Apache user who started the process.

Run an exec shell with another non-apache user.

http://phpseclib.sourceforge.net/ see this lib that can solve your problem.

  • This alternative is very good @Alessandro and I’m using it at the moment, but I’m having a security problem since I haven’t found a way to run it without having to put the user and root password in the script. I’m doing a lot of research on this. There is a way that would add a "permission" in the /etc/sudoers for password-free access, but so far I have not been able to run phpseclib this way and without the use of password.

0

An elegant solution would be to use the command nohup linux.

With this command you can leave a process running in the background and it does not die when you leave. Works very well for ssh sessions, and should work also for your case.

  • I used this command: exec('nohup ./sc_serv /path-do-conf/sc_serv.conf </dev/null 2>/dev/null >/dev/null & echo $!'); started the process normally, but restarted apache the process was gone. Unfortunately I still have the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.