Jump to content

Getting ProcessID


RuleBritannia

Recommended Posts

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 by RuleBritannia
Link to comment
Share on other sites

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 by RuleBritannia
Link to comment
Share on other sites

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 by RuleBritannia
Link to comment
Share on other sites

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 free
As 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.
Link to comment
Share on other sites

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 free
As 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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.