Get real PID of shell process

I use the below code to get the real PID number of a launched Shell.

var PIDterminal as new shell
PIDterminal.Execute("pgrep " + filename + " | tail -1")
do
loop until not PIDterminal.IsRunning

Return PIDterminal.ReadAll

This works fine. But of course not when I sandbox it (for AppStore).
Is there another way to retrieve the real PID number?

BTW the shell.PID property provided by Xojo is not the correct one !! So that’s no use.

I have not yet heard this, do you have details on the situation?

Well, the PID property returned is not the same as the one you get in the Activity Monitor. That’s a very long standing known issue.

Anyhow, in the meantime I found it with using MBS.

2 Likes

On Windows, Shell.PID returns a handle to the process rather than the PID. It’s probably something similar on Mac.

Interesting, I was not aware! Thanks for the heads up and the details.

I use the following code to get the PID:
// ------------------------------------------------------------------------------------------------------------
// - GetPID (Methode aus den XOJO-Examples kopiert) -
// ------------------------------------------------------------------------------------------------------------
// This code gets the current process ID and illustrates soft declares
// for Mac OS, Windows, and Linux.
// In addition, it illustrates the use of the Alias parameter to set the
// name of the declared function to the same value for all platforms.
// ------------------------------------------------------------------------------------------------------------
// <-- Prozeß-Identifikation
// ------------------------------------------------------------------------------------------------------------
#If TargetMacOS
// This is not the same as a Mac OS PSN.
Soft Declare Function GetProcessID Lib “libc.dylib” alias “getpid” As Integer
#EndIf

#If TargetWin32
Soft Declare Function GetProcessID Lib “Kernel32” alias “GetCurrentProcessId” As Integer
#EndIf

#If TargetLinux
Soft Declare Function GetProcessID Lib “libc.so” alias “getpid” As Integer
#EndIf

Return GetProcessID

It is a copy out of the XOJO example Advanced->Declares->DeclareGetPID.Xojo_binary_project.

3 Likes

That will return the .app PID. Not the Shell PID.

Don’t forget to check ShellMBS and WindowsProcessMBS in MBS Xojo Plugins for more options than built-in Shell class.