0

Let’s say we have an app called qBittorrent that allows us to run a shell script upon completion of a torrent download. While writing that shell script, to be invoked by qBittorrent, we realize it would be handy to have the PID of the app that called the shell script - in this case qBittorrent.

How do I achieve this in bash, zsh or sh?

I have read a few hints here and come across $PPID. On macOS this gives me etc/launchd not qBittorrent. It could be that qBittorrent is calling etc/launchd to run the shell script…

in that case would I need to return a list of parent processes and how could I achieve this?

Tried:

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

Returns:

/sbin/launchd

ps -o comm= >> /Users/john/Desktop/log.txt 2>&1

Returns:

/bin/zsh
-zsh
-zsh
-zsh
-zsh

I happen to have four terminal windows active when this script ran.

John
  • 469
  • 1
  • 4
  • 17
  • 1
    What exactly works depends on your OS. In very brief, you can parse the output of `ps´ to traverse the parent PID links in the generated output, but the precise formatting and what options to use is widely system-dependent. – tripleee Apr 25 '23 at 05:31
  • I don't have qBitTorrent, but the symptoms you describe sound like qBitTorrent uses `launchd` specifically to detach itself from the process it asks `launchd` to launch, probably so the parent can exit and leave the (pseudo-) child running indefinitely under the LaunchD daemon and not worry about waiting for it etc. – tripleee Apr 25 '23 at 05:47
  • There might be an option to `launchctl` which allows you to search for what started a particular job but this is outside of my expertise. https://stackoverflow.com/questions/56573815/list-all-loaded-unloaded-or-both-launch-agents-on-macos looks vaguely like a useful starting point. – tripleee Apr 25 '23 at 05:52
  • @tripleee I modified the question after attempts with `ps` – John Apr 25 '23 at 17:47
  • That doesn't change the basic problem. If qBitTorrent doesn't directly start a child, but asks LaunchD to start a child, the child process will not have a useful parent PID, and you need a different mechanism to support your use case. – tripleee Apr 25 '23 at 18:50

0 Answers0