1

I am running an app that calls script.sh and I would like to retrieve the invoking app's PID or name within that same script.sh. Ultimately, in my script.sh, I am trying to determine whether the app or Terminal was the invoker (in order to run different commands)

I've added this to my script.sh

ps -p $PPID -o ucomm= >> /Users/john/Desktop/log.txt 2>&1

It seems the app is using etc/launchd to open my default shell (zsh) to run the script and not waiting... I believe this is called a "zombie process"?

man ps says

When printing using the command keyword, a process that has exited and has a parent that has not yet waited for the process (in other words, a zombie) is listed as “”, and a process which is blocked while trying to exit is listed as “”. If the arguments cannot be located (usually because it has not been set, as is the case of system processes and/or kernel threads) the command name is printed within square brackets. The process can change the arguments shown with setproctitle(3). Otherwise, ps makes an educated guess as to the file name and arguments given when the process was created by examining memory or the swap area. The method is inherently somewhat unreliable and in any event a process is entitled to destroy this information. The ucomm (accounting) keyword can, however, be depended on. If the arguments are unavailable or do not agree with the ucomm keyword, the value for the ucomm keyword is appended to the arguments in parentheses.

(Emphasis mine)

However I don't see "defunct" or, when using the flag ucomm, anything listed as mentioned in the man page.

Is there another set of flags or options I can try with ps to get more information?

1
  • It might be easier to have you application define an environment variable or pass a special argument, and then have the script check for those.
    – nohillside
    Apr 28, 2023 at 7:30

1 Answer 1

4

I think you might be confusing two concepts here - althought it is difficult to tell as you haven't got the actual output from ps included.

In your question you talk about "zombie processes". Zombie processes are child programs that have ended execution, but the parent process has not yet collected the return value of that execution. So if your script.sh file is executing it cannot by definition be a zombie process.

I think your process is actually an "orphan process". Orphan processes are programs where the parent process has ended execution, but the child process is still executing. In those cases, the parent process is actually "gone", and the process with ID 1 takes over as a new parent of the child process. The process with ID 1 on a standard installation of macOS is launchd.

So when you check the parent of your process and find that it is launchd, it could just mean that the actual parent has finished executing earlier on, and now launchd has taken over as the new parent.

1
  • Thank you for that description and edification. Indeed when I include that short command I see PID "1" and "etc/launchd". That should be enough for me.
    – John
    Apr 28, 2023 at 16:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .