How to get a PID of Windows process called with $zf(-1,...)?
Hello everybody,
We have a piece of Caché software which calls an external utility using $zf(-1,command). It works fine under Linux, but under Windows an external process occasionally hangs (due to some internal problems out of the scope here) and need to be killed programmatically. Having PID, it's easy to kill a process. If a Caché process is called with JOB command, the caller can easily get its PID from $zchild, but alas $zf(-1) does not seem to return the similar info. Is it possible to get it somehow?
I don't know of any magic $zf/$zu function or API to do this. Looking at StackOverflow, a common approach seems to be parsing the output of "tasklist" filtered by executable name and/or window title.
Here's a classmethod to get the PIDs of all processes with a given executable name (Windows-only, of course):
Thank you, Timothy.
Definitely this is a solution, while our case is a bit more complex. It can be several running copies of "dangerous" utility started by different users, so we can't select the actual one neither by executable name nor by its window title as it is started with "-nogui" option and has got no window.
An approach how to bypass this limitation that we are going to implement looks like this:
Thank you, Robert.
In this case the PID saved in a file will be the PID of command processor (cmd) itself rather than the PID of an utility which it has invoked. If Cache process kill the cmd instance using this PID, the utility will continue its execution. Besides, there is no parent-child relationship between the cmd and the utility, so /t switch (kill process tree) would not help.
Alexey,
with $ZF(-1,command) you are at OS level.
Nothing prevents you from extending command to a script that writes its pid in a file.
using $JOB_".pid" or similar as file name should be enough to identify it.
So after a moderate HANG your pid should be available.
I prefer this to using pipes or other sophisticated solutions (e.g. listening on a TCP/IP or UDP port, ...)
as you get an embedded trace for free
HTH