PID of console app

When my console app launches, I want to record its pid. How can I discover that without plugins? (Mac and Linux, primarily, though Windows would be nice too.)

I should point out that there will be several instances of my console app running so I need a way to determine the current one.

This is a bit convoluted, but it might work:

  1. When the app starts, spin up a ServerSocket instance and have it listen on a weird random port.
  2. Drop out to the shell and run: lsof -i :[[The Random Port]]
  3. Stop the ServerSocket.

That should give you the PID of that specific instance of the app.

Thanks for the suggestion, but there has to be an easier way, no? I mean, console apps report their own pid all the time.

Agreed. There has to be an easier way.

I know that you said “no plugins,” but the MBS Linux Plugin’s “LinuxProcessMBS” class can give you the PID (and a bunch of other interesting info - StartTime, Thread Count, etc). I haven’t checked, but there’s very likely similar classes available for Mac and Windows.

Linux is easy:

Dim f As new FolderItem("/proc/self") ' /proc/self is a symlink to /proc/[pid] Dim pid As Integer = Val(f.DisplayName)

Here’s a cross platform piece of code to get the PID of the current process:

The macos declares were kindly posted by Sam Rowlands here which I modified to add processIdentifier

Tested in Windows 10, macOS Sierra and Linux Mint 18.3 Cinnamon

That looks like what I need, thanks!

No probs, I just had a read up on some things (haven’t used macos declares much) and added a compact version that doesnt need the included classes. Re-download if you want to take a look.

/facepalm There’s a crossplatform demo of this in File>New Project>Examples>Advanced>Declares>DeclareGetPID

Ahh well at least I learned how to use macOS declares :wink:


Thanks for finding that @ . I never think to look in the examples. :confused:

Oh, this gets worse. It turns out that we had copied that code (or some version of it) into a project helper module some time ago, and I never thought to check.

Excuse me while I beat my head against a door…