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.