To be honest, I think there is definitely a better way. But leave it as a memorandum
--What you did: Get the PID and kill it forever in an infinite loop
I want to keep killing a specific process that revives even if I kill it. The process seems to start regularly in the background. Environment: macOS Catalina
Open Activity Monitor from Applications> Utilities and look up the process name.
Now that you know the process name, organize what you want to do. What i want to do
--Kill and continue killing a specific process that revives immediately
about it. So as a process
--ps aux | grep ProcessName to see if the process is running and kill it if it is
That seems to be okay. Let's write a script
#!/bin/bash
while true;
do
#Enter the name of the process you want to kill in ProcessName
TARGETPID=`ps aux | grep ProcessName | grep -v grep | awk '{print $2}'`
if [ -n "$TARGETPID" ]; then
`kill -9 ${TARGETPID}`
fi
sleep 5
done
As you can see, it's just an infinite loop. (Somehow I tried to make it every 5 seconds)
--check the process with ps aux | grep ProcessName --grep -v grep excludes own results --Awk'{print $ 2}' extract only PID -Store the result of ↑ in a variable once, kill if it is not empty
I am doing that. I do it every 5 seconds, but I think it's okay if it's about this kind of processing, such as every 1 second or no interval.
Operation Not Permitted
This message might be displayed when trying to kill. In such a case, you may try kill with sudo once. (sudo kill or sudo kill -9)
#Enter the ID of the process you want to kill in PID
sudo kill PID
#Or
sudo kill -9 PID
If that doesn't work, you may be able to kill it by logging in again as the mac root user and running it as that user ... (I can't say anything because I haven't confirmed it)
Recommended Posts