Why does PID of apps change/Is there a pattern?

0

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?

Rhett Henderson

Posted 2019-02-21T16:30:37.290

Reputation: 1

1Why does iTunes keep starting up? This seems like an inefficient method of continually trying to stop the iTunes process. – DrZoo – 2019-02-21T16:56:18.587

My TV remote logs in a random user and starts playing a song on iTunes whenever we press "play" and there's not a way to fix that without reconfiguring the entire TV remote, which would take hours. This way I can just run this program while we're watching TV and it will practically fix the problem. – Rhett Henderson – 2019-02-21T17:03:27.230

Interesting. Is putting the computer to sleep not an option? On another note, this is on a Mac right? – DrZoo – 2019-02-21T17:13:08.253

Yes this is on a Mac, and putting the computer to sleep is an option, and the app still opens even if the computer is put to sleep. – Rhett Henderson – 2019-02-21T17:22:56.823

1

You would probably be better off disabling the iTunes Helper - see https://apple.stackexchange.com/questions/91710/itunes-helper-still-running-even-if-it-is-off

– Tetsujin – 2019-02-21T18:49:29.467

Answers

0

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.

DrZoo

Posted 2019-02-21T16:30:37.290

Reputation: 8 101

launchd is always PID 1, kernel_task is PID 0. Are you thinking nix rather than Mac? The rest, as you say, will always be sequential numbers - in effect 'random'. – Tetsujin – 2019-02-21T18:45:58.003

1@Tetsujin yeah, I was thinking nix. I think your comment is the better option if that stops it from opening automatically. – DrZoo – 2019-02-21T19:12:07.033