[LINUX] 何度も蘇るプロセスを永遠に殺し続ける.sh

To be honest, I think there is definitely a better way. But leave it as a memorandum

Summary for those who don't want to waste time

--What you did: Get the PID and kill it forever in an infinite loop

Thing you want to do

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

First, look up the process name

Open Activity Monitor from Applications> Utilities and look up the process name. process_mosaic.png

Try

Organize the contents

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

Implementation

#!/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

何度も蘇るプロセスを永遠に殺し続ける.sh