私はグーグルのおかげでここに着陸し、この10年前の投稿には、PHPでプロセスを呼び出し/開始し、プロセスIDを使用してそれを強制終了する方法に基づいて、より多くの情報が必要であると判断しました...
コマンドを実行するとします (この例では、ffmpeg を使用して Windows システム上のファイルを rtmp サーバーにストリーミングします)。次のようなコマンドを実行できます。
ffmpeg -re -i D:\wicked_video.mkv -c:v libx264 -preset veryfast -b:v 200k -maxrate 400k -bufsize 6000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 160k -ac 2 -ar 44100 -f flv rtmp://<IP_OF_RMTP_SERVER>/superawesomestreamkey > d:\demo.txt 2> d:\demoerr.txt
最初の部分はここで説明され、そのコマンドの最後の部分は、ロギング目的でその名前のファイルに出力されます。
> d:\demo.txt 2> d:\demoerr.txt
したがって、そのコマンドが機能すると仮定しましょう。あなたはそれをテストしました。そのコマンドをphpで実行するには、execで実行できますが、時間がかかります(別の主題、set_time_limitを確認してください)。これは、php経由でffmpegによって処理されるビデオです。行くべき道ではありませんが、この場合は起こっています。
コマンドをバックグラウンドで実行できますが、そのプロセスの pid は何ですか? 何らかの理由でそれを強制終了したいのですが、psexec はユーザーによって実行された「プロセス ID」のみを提供します。この場合、ユーザーは 1 人しかいません。同じユーザーに複数のプロセスが必要です。
以下は、php で実行中のプロセスの pid を取得する例です。
// the command could be anything:
// $cmd = 'whoami';
// This is a f* one, The point is: exec is nasty.
// $cmd = 'shutdown -r -t 0'; //
// but this is the ffmpeg example that outputs seperate files for sake
$cmd = 'ffmpeg -re -i D:\wicked_video.mkv -c:v libx264 -preset veryfast -b:v 200k -maxrate 400k -bufsize 6000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 160k -ac 2 -ar 44100 -f flv rtmp://10.237.1.8/show/streamkey1 > d:\demo.txt 2> d:\demoerr.txt';
// we assume the os is windows, pipe read and write
$descriptorspec = [
0 => ["pipe", "r"],
1 => ["pipe", "w"],
];
// start task in background, when its a recource, you can get Parent process id
if ( $prog = is_resource( proc_open("start /b " . $cmd, $descriptorspec, $pipes ) ) )
{
// Get Parent process Id
$ppid = proc_get_status($prog);
// this is the 'child' pid
$pid = $ppid['pid'];
// use wmic to get the PID
$output = array_filter( explode(" ", shell_exec("wmic process get parentprocessid,processid | find \"$pid\"" ) ) );
array_pop($output);
// if pid exitst this will not be empty
$pid = end($output);
// outputs the PID of the process
echo $pid;
}
上記のコードは、「inBackground」で実行されたプロセスの pid をエコーする必要があります。
pid がまだ実行されている場合は、後で kill するために pid を保存する必要があることに注意してください。
これでプロセスを強制終了できます: (pid が 1234 であると想像してください)
//'F' to Force kill a process
exec("taskkill /pid 1234 /F");
これがstackoverflowに関する私の最初の投稿です。これが誰かの助けになることを願っています。さみしくない素敵なクリスマスを♪♪