Find the PID(s) of running processes and store as an array

前端 未结 4 1470
被撕碎了的回忆
被撕碎了的回忆 2021-01-12 03:01

I\'m trying to write a bash script to find the PID of a running process then issue a kill command. I have it partially working, but the issue I face is that there may be mor

4条回答
  •  长发绾君心
    2021-01-12 03:38

    Here's a little one liner that might help

    for pid in `ps -ef | grep your_search_term | awk '{print $2}'` ; do kill $pid ; done
    

    Just replace your_search_term with the process name you want to kill.

    You could also make it into a script and swap your_search_term for $1

    EDIT: I suppose I should explain how this works.

    The back ticks `` collects the output from the expression inside it. In this case it will return a list of pids for a process name.

    Using a for loop we can iterate through each pid and kill the process.

    EDIT2: replaced kill -9 with kill

提交回复
热议问题