Access the PID of a process spawned by IDL (UNIX / Linux / Mac OS X) before SPAWN returns
QUESTION -
"On UNIX, Linux or Mac OS X, how can I SPAWN a separate program from IDL without waiting for that program to return and then be able to access the PID of that spawned program?"
Normally, if a user can wait for the IDL "SPAWN" procedure to return, the PID keyword to SPAWN can be used to get the spawned child process ID (PID).
However, in the case where the spawned program must run synchronously with IDL, i.e., in the foreground while IDL execution waits, but the user would like to get the child PID before the child process returns control back to IDL, then an alternative approach is needed to get this information.
SOLUTION -
One approach might be to SPAWN a pair of commands:
1.) First, print the PID of the spawned child shell to the terminal ("echo $$").
2.) Next, "exec" the target command in order to replace the child shell spawned by the IDL with the target command. For example:
IDL> SPAWN, 'echo $$ ; exec xclock', PID=pid
26023
At this point, the PID of the child process is printed to screen and the user can then use PID information to access that exact child process outside of the context of IDL, while the spawned process continues to run synchronously with the IDL session.
Subsequently when the "xclock" window is closed, control returns back to the IDL prompt. At that point you will have access to the child "pid" variable that is returned to the PID keyword assignment to the "pid" variable by SPAWN:
IDL> PRINT, pid
26023