ThomasIsCoding
ThomasIsCoding

Reputation: 102529

How to get the PID of an external program (invoked by MATLAB) via MATLAB commands?

I am curious how to get the PID of an external program invoked by MATLAB (in Windows).

For instance, I invoke a notepad in MATLAB via the command !notepad.exe or system('notepad.exe'). I want to obtain the PID of this notepad immediately once it is invoked.

Since multiple notepads might be open at the same time on one computer, I need to get their respective PIDs (instead of the process name) to keep track of them. I have no clue how it can be implemented....

looking for help, thanks in advance!

Upvotes: 3

Views: 675

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112749

Creation date not needed

You can call Windows' tasklist command from Matlab using system, and then parse the results:

name = 'notepad.exe';
[~, s] = system(['tasklist /FI "imagename eq ' name '"']);
result = regexp(s, ['(?<=' strrep(name, '.', '\.') '\s*)\d+'], 'match');
result = str2double(result); % convert to numbers if needed

The result of system is of the following form (two Notepad windows open; Spanish Windows version):

>> s
s =
    '
     Nombre de imagen               PID Nombre de sesión Núm. de ses Uso de memor
     ========================= ======== ================ =========== ============
     notepad.exe                  12576 Console                    1    13,488 KB
     notepad.exe                  13860 Console                    1    13,484 KB
    '

So the regular expression searches for digits preceded by the program name and optional spaces, to give the final result

>> result =
          12576       13860

Creation date needed

If you need to filter based on creation date, you can use Windows' wmic:

name = 'notepad.exe';
[~, s] = system(['wmic process where name=''' name ''' get ProcessId, CreationDate']);

This gives a string such as

s =
    'CreationDate               ProcessId  
     20191015151243.876221+120  6656       
     20191015151246.092357+120  4004       

     '

The CreationDate is in the format yyyymmddHHMMSS+UUU, where +UUU or -UUU is number of minutes from UTC.

You can parse s into a cell array of strings as follows:

result = reshape(regexp(s, '[\d+\.]+', 'match').', 2, []).'; % 2 is the number of columns

This gives

result =
  2×2 cell array
    {'20191015151243.876221+120'}    {'6656'}
    {'20191015151246.092357+120'}    {'4004'}

Then you can filter based on the first columnn.

Upvotes: 3

Paolo
Paolo

Reputation: 26220

Create a Powershell script, findPid.ps1, containing the following:

Get-Process | Where {$_.ProcessName -eq "notepad"} | Sort-Object starttime -Descending | Select 'Id'

The script above gets the information on the running notepad processes, filters them by time and extracts the pid.


Execute a non-blocking system call from MATLAB:

system('notepad.exe &')

Call the Powershell script:

[~,pids] = system('powershell -file findPid.ps1');

pids is a char vector containing the pids of the notepad.exe process (or processes).

So to obtain the most recent pid:

pid = regexp(pids,'Id\n[^-]+--\n([0-9]+)','tokens','once')

Upvotes: 2

Related Questions