terminal.app - terminal.app - Why does PID of apps change/Is there a pattern? - answerstu - answerstu.com answerstu

terminal.app - Why does PID of apps change/Is there a pattern?

Long story short, I'm trying to write a program in Python that will continuously run the command kill [PID of iTunes but from some testing with the command ps aux | grep iTunes | egrep -v "grep|Helper" | awk '{print $2}' and the PID of iTunes changes every time and I can't seem to find a pattern to it. Is there a pattern to it, or way to make it not increment?

1 Answer

  1. Randolph- Reply

    2019-11-15

    There is only one process that will have the same PID each time, on any session or system. That is the init process with will always have the PID 1. Other than that, there is no pattern.

    If you use the Homebrew command brew install proctools it will download, build, and install pgrep.

    Then you could use pgrep -f <process name> | awk '{print "kill -9 " $1}'

    I believe another option would be using pkill with the process name. In that case, I don't think you would have to know the process ID, just the process name. pkill would also be installed if you did the Homebrew command listed above.

    If you don't want to install anything, try running this ps axf | grep <process name> | grep -v grep | awk '{print "kill -9 " $1}' | sh. See what it prints out in the shell to see if it will be killing the correct process.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>