Hi,
i'm trying to spawn a daemon-like process from php on a unix environment, keep the pid for reference and keep it completely independent from httpd processes. This is useful for controlling middleware applications from administrative backoffices. I want to keep it generalized to reuse for any command, i don't want to write a .sh for every command.
using php's exec() i'm running this command:
$command = "java /myApp/TestInfiniteLoop";
$log = "/myApp/myLog.txt";
$echos = null;
$fullCommand = "bash -c 'nohup $command < /dev/null > $log 2>&1 & echo $! & exit' & echo $!";
exec($fullCommand, $echos);
$pid = (int)$echos[0] + 2;
It returns $pid but does not spawn process (or kills it immediately...).
If i run the $fullCommand on a bash shell it works perfectly. I can close the bash and everything keeps running as expected.
If i take out the "nohup " it works (but pid is +1 and not +2), but as soon as you stop httpd the process get's killed too...
What is the correct way to make it work?
If possible can anyone correct it to get the pid directly? i'm assuming it is the second process from the bash (+2) call, but it is possible (though not probable, i know...) that it wouldn't be the same.
PS: i've been testing it on Mac and Linux environments.