RuleBritannia Posted November 11, 2013 Share Posted November 11, 2013 (edited) I am running a process, Via COM class and Run method, However to get the ProcessID seems impossible. $com = new COM('WScript.Shell'); $d = $com->Run('cmd /C C:\ffmpeg -y -i "C:\2.avi" -preset slow -crf 25 "C:\2.mkv" 1>C:\output.log 2>C:\error.log',0,false); I believe there is a property called ProcessID from my research I have tried var_dump($com->ProcessID); Error : Fatal error: Uncaught exception 'com_exception' with message 'Unable to lookup `ProcessID': Unknown name. also tried var_dump($d->ProcessID); Error : Notice: Trying to get property of non-object in Im assuming the main place to get this would be from where it starts ($com), But as you can see doesnt work. When I var_dump($com) it shows just that its a object. var_dump($com) Result : object(com)#2 (0) {} Is this even possible to do? Thanks in advance EDITED while I can I forgot to mention, I am also reading about $com->Exec instead of $com->Run to see if there is a possiblity there, But would rather use run and find the answer to get ProcessID from that, But so far not possible from my research. Edited November 11, 2013 by RuleBritannia Quote Link to comment https://forums.phpfreaks.com/topic/283802-getting-processid/ Share on other sites More sharing options...
RuleBritannia Posted November 11, 2013 Author Share Posted November 11, 2013 (edited) So here is one possible solution to get the ProcessID, But involves using $com->Exec instead of $com->Run And the problem with this is you can not supply much parameters(such as hide window, or hang php until command finish)etc. $com = new COM('WScript.Shell'); $d = $com->Exec('cmd /C C:\ffmpeg -y -i "C:\2.avi" -preset slow -crf 25 "C:\2.mkv"); echo $d->ProcessID When this runs, It will open up a command window, But actually shows nothing interesting whatsoever, then when its complete it will auto close. Using the Run property, If you tell it to open a command window, It will show all interesting information such as ffmpeg information etc, So I am still interested to find a possible solution using Run, I will research more and get back here. Thanks Edited November 11, 2013 by RuleBritannia Quote Link to comment https://forums.phpfreaks.com/topic/283802-getting-processid/#findComment-1457860 Share on other sites More sharing options...
trq Posted November 11, 2013 Share Posted November 11, 2013 People actually use PHP on Windows? Quote Link to comment https://forums.phpfreaks.com/topic/283802-getting-processid/#findComment-1457863 Share on other sites More sharing options...
RuleBritannia Posted November 11, 2013 Author Share Posted November 11, 2013 People actually use PHP on Windows? Its funny you say that, Because everywhere I am looking online regarding running processes in the background, Seem to use linux examples. still researching Quote Link to comment https://forums.phpfreaks.com/topic/283802-getting-processid/#findComment-1457864 Share on other sites More sharing options...
RuleBritannia Posted November 11, 2013 Author Share Posted November 11, 2013 Pffft. Doesnt seem like this is possible with Run. Quote Link to comment https://forums.phpfreaks.com/topic/283802-getting-processid/#findComment-1457884 Share on other sites More sharing options...
RuleBritannia Posted November 11, 2013 Author Share Posted November 11, 2013 For those who it may concern, Some more info here. http://stackoverflow.com/questions/13289595/starting-a-windows-process-in-php-and-get-its-pid This guy had the exact same issue, seems its not possible, but a work around he posted. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/283802-getting-processid/#findComment-1457897 Share on other sites More sharing options...
RuleBritannia Posted November 11, 2013 Author Share Posted November 11, 2013 (edited) Sorry. Cannot edit, But 1 other alternative if others are interested http://stackoverflow.com/questions/5367261/php-exec-as-background-process-windows-wampserver-environment seems only way to do this in the background + without application windows opening is with popen or proc_open All else its impossible to get Process ID without application window opening. Edited November 11, 2013 by RuleBritannia Quote Link to comment https://forums.phpfreaks.com/topic/283802-getting-processid/#findComment-1457933 Share on other sites More sharing options...
requinix Posted November 11, 2013 Share Posted November 11, 2013 Exec() is a bit more powerful than you may expect. As a demonstration, $com = new COM("WScript.Shell"); $exec = $com->Exec("cmd /c dir C:\\"); // cmd /c because I want dir which is a cmd command, not an actual program // $exec = $com->Exec("tasklist") for a regular program // from here on you should treat it like a regular process // that includes things like closing stdin when you're done sending input (if any) $exec->StdIn->Close(); echo "Listing everything under C:\\ with a command prompt.\n"; echo "Process ID: {$exec->ProcessID}\n"; $stdout = $exec->StdOut; $line = 1; while (!$stdout->AtEndOfStream) { echo " {$line} {$stdout->ReadLine()}\n"; $line++; } Listing everything under C:\ with a command prompt. Process ID: 6708 1 Volume in drive C is OS 2 Volume Serial Number is 842F-D8EC 3 4 Directory of C:\ 5 6 04/04/2013 10:47 PM <DIR> apps 7 05/14/2013 08:13 AM <DIR> dell 8 04/05/2013 12:10 AM <DIR> Drivers 9 04/04/2013 10:38 PM <DIR> Intel 10 05/20/2013 01:19 PM <DIR> msysgit 11 07/13/2009 07:20 PM <DIR> PerfLogs 12 10/23/2013 01:24 PM <DIR> Program Files 13 10/16/2013 12:57 PM <DIR> Program Files (x86) 14 10/08/2013 07:22 PM <DIR> Temp 15 05/15/2013 11:11 AM 31 tmuninst.ini 16 07/05/2013 11:41 PM <DIR> Users 17 10/11/2013 09:20 AM <DIR> Windows 18 1 File(s) 31 bytes 19 11 Dir(s) 711,940,968,448 bytes freeAs this shows, you don't need output redirection at all since you now have direct access to the output streams. $input = "C:\\2.avi"; $output = "C:\\2.mkv"; $com = new COM("WScript.Shell"); $exec = $com->Exec("C:\\ffmpeg -y -i " . escapeshellarg($input) . " -preset slow -crf 25 " . escapeshellarg($output)); $pid = $exec->ProcessID; $exec->StdIn->Close(); $output = $exec->StdOut->ReadAll(); $error = $exec->StdErr->ReadAll(); HOWEVER, after all this work, all you've really gone and done is written a Windows-only version of PHP's built-in, cross-platform proc_open (with proc_get_status() to get the PID). So yes, you should probably be using that instead of all this COM stuff. Quote Link to comment https://forums.phpfreaks.com/topic/283802-getting-processid/#findComment-1457937 Share on other sites More sharing options...
RuleBritannia Posted November 11, 2013 Author Share Posted November 11, 2013 Exec() is a bit more powerful than you may expect. As a demonstration, $com = new COM("WScript.Shell"); $exec = $com->Exec("cmd /c dir C:\\"); // cmd /c because I want dir which is a cmd command, not an actual program // $exec = $com->Exec("tasklist") for a regular program // from here on you should treat it like a regular process // that includes things like closing stdin when you're done sending input (if any) $exec->StdIn->Close(); echo "Listing everything under C:\\ with a command prompt.\n"; echo "Process ID: {$exec->ProcessID}\n"; $stdout = $exec->StdOut; $line = 1; while (!$stdout->AtEndOfStream) { echo " {$line} {$stdout->ReadLine()}\n"; $line++; } Listing everything under C:\ with a command prompt. Process ID: 6708 1 Volume in drive C is OS 2 Volume Serial Number is 842F-D8EC 3 4 Directory of C:\ 5 6 04/04/2013 10:47 PM <DIR> apps 7 05/14/2013 08:13 AM <DIR> dell 8 04/05/2013 12:10 AM <DIR> Drivers 9 04/04/2013 10:38 PM <DIR> Intel 10 05/20/2013 01:19 PM <DIR> msysgit 11 07/13/2009 07:20 PM <DIR> PerfLogs 12 10/23/2013 01:24 PM <DIR> Program Files 13 10/16/2013 12:57 PM <DIR> Program Files (x86) 14 10/08/2013 07:22 PM <DIR> Temp 15 05/15/2013 11:11 AM 31 tmuninst.ini 16 07/05/2013 11:41 PM <DIR> Users 17 10/11/2013 09:20 AM <DIR> Windows 18 1 File(s) 31 bytes 19 11 Dir(s) 711,940,968,448 bytes freeAs this shows, you don't need output redirection at all since you now have direct access to the output streams. $input = "C:\\2.avi"; $output = "C:\\2.mkv"; $com = new COM("WScript.Shell"); $exec = $com->Exec("C:\\ffmpeg -y -i " . escapeshellarg($input) . " -preset slow -crf 25 " . escapeshellarg($output)); $pid = $exec->ProcessID; $exec->StdIn->Close(); $output = $exec->StdOut->ReadAll(); $error = $exec->StdErr->ReadAll(); HOWEVER, after all this work, all you've really gone and done is written a Windows-only version of PHP's built-in, cross-platform proc_open (with proc_get_status() to get the PID). So yes, you should probably be using that instead of all this COM stuff. Hello Yes, i already managed to get PID with this before I moved onto proc_open, The problem with this is you cannot hide the command window if I remember correctly.. Which I had to do, proc_open hides it, but permenantly, so its the opposite. Unless I dont know the full funcionality but I researched and tested alot. Quote Link to comment https://forums.phpfreaks.com/topic/283802-getting-processid/#findComment-1457953 Share on other sites More sharing options...
requinix Posted November 11, 2013 Share Posted November 11, 2013 The problem with this is you cannot hide the command window if I remember correctly..I haven't been able to get it to appear at all. Are you sure that with Exec() and running ffmpeg directly (not cmd) it appears for you? Quote Link to comment https://forums.phpfreaks.com/topic/283802-getting-processid/#findComment-1457956 Share on other sites More sharing options...
RuleBritannia Posted November 12, 2013 Author Share Posted November 12, 2013 I haven't been able to get it to appear at all. Are you sure that with Exec() and running ffmpeg directly (not cmd) it appears for you? Cant remember specifically without cmd, but it was definatly appearing with my cmd /C ffmpeg......... commands, And i could not hide it. after all of these differnet methods and so on, i finally just went with shell_exec in a external php script, then called it via curl and timeout 1(to ignore result info) I guess I still learnt alot from this, but it has gave me a big headache, and caused me to change the script so many times. Thansk for all your input Quote Link to comment https://forums.phpfreaks.com/topic/283802-getting-processid/#findComment-1458042 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.