I have this php script "start_stream.php", which starts a process (ffmpeg) using proc_open() and then stays in a loop checking for a keep-alive signal.
Once the keep-alive signal is too old, the php script will terminate the process by its pID with this command:
$output = system('taskkill /F /PID '.$this->pID);
echo 'TASKKILL OUTPUT: ';
var_dump($output);
The output from this is always like this:
TASKKILL OUTPUT: string(55) "SUCCESS: The process with PID 2272 has been terminated."
BUT under some circumstances the process does not terminate, but keeps running. But I always get the SUCCESS message.
If I run the start_stream.php script from cmd (windows) with this command:
php start_stream.php --stream [STREAM-NAME] --url [STREAM-URL]
Then the process will terminate properly as expected.
But since this is a web-application, I need to call the start_stream.php script from another php script. I do it like this:
//Set the command for streaming
$command = 'php start_stream.php --stream '.$stream.' --url '.$url;
//Add the environment variables for shell execution
putenv('PATH=' . $_SERVER['PATH']);
//Set the working directory to streaming directory
chdir("..\\streaming");
//Start the streaming process
define('STDIN',fopen("php://stdin","r"));
define('STDOUT',fopen("stdout-".$stream,"w"));
$descriptorspec = [STDIN, STDOUT, STDOUT];
$process = proc_open($command, $descriptorspec, $pipes);
When I do this, I still get the SUCCESS output, stating that the process has terminated, but the process keeps running in the background.
It works to kill the process with this:
shell_exec('taskkill /IM "ffmpeg.exe" /F');
But I cannot use that, since it will kill all ffmpeg processes, and I need to run concurrent processes, killing only the relevant ones.
As I said earlier, the script runs and terminates as intended if I use the following command on the command line:
php start_stream.php --stream [STREAM-NAME] --url [STREAM-URL]
But if I use php.exe instead of php like so:
php.exe start_stream.php --stream [STREAM-NAME] --url [STREAM-URL]
I will have the same problem with the ffmpeg process not terminating, but still giving a success message.
I am also using proc_terminate() in the start_stream.php script like so:
proc_terminate($this->process);
But this also gives me a "process terminated" status response while the process does not stop.
The pID for the process is the pID for the cmd instance running the process, not for the ffmpeg process itself. When I call the termination of this process, the cmd process is killed in every instance. When calling the php process from cmd without using php.exe, the ffmpeg process is killed along with the cmd process. When using php.exe, only the cmd process is killed, but the ffmpeg process keeps running.
Is there a way to either ensure that the ffmpeg process is killed along with the cmd process, or maybe some way to get the pID of the ffmpeg process itself in the context that I need?