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?
-
1Why does iTunes keep starting up? This seems like an inefficient method of continually trying to stop the iTunes process.– DrZooCommented Feb 21, 2019 at 16:56
-
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 HendersonCommented Feb 21, 2019 at 17:03
-
Interesting. Is putting the computer to sleep not an option? On another note, this is on a Mac right?– DrZooCommented Feb 21, 2019 at 17:13
-
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 HendersonCommented Feb 21, 2019 at 17:22
-
1You would probably be better off disabling the iTunes Helper - see apple.stackexchange.com/questions/91710/…– TetsujinCommented Feb 21, 2019 at 18:49
1 Answer
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.
-
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'.– TetsujinCommented Feb 21, 2019 at 18:45 -
1@Tetsujin yeah, I was thinking nix. I think your comment is the better option if that stops it from opening automatically.– DrZooCommented Feb 21, 2019 at 19:12