0

I want to get pid of a process and then kill it. When I do ps -ef | grep "python3 bot.py" I have this output:

root       43903       1  0 Jun26 ?        00:03:28 python3 bot.py
root       48808   48298  0 17:57 pts/0    00:00:00 grep --color=auto python3 bot.py

and I want to get the PID 43903. How can I accomplish this using bash script?

So far I have written:

ps -ef | grep "python3 bot.py" | awk "NR==1 {print $1}"

which outputs

root       43903       1  0 Jun26 ?        00:03:28 python3 bot.py

But now when I rerun awk on it, it has no output. How can I get the 43903 from this line?bash

a6623
  • 137

1 Answers1

1

Try this:

ps ax | grep "python3 bot.py" | cut -f2 -d" " - | xargs kill

The first two pipes get process information, then we try to get the PID column, and finally, we kill the resulting PID.

Alternatively, this should also work:

kill $(pgrep -f 'python3 bot.py')

Hope this helps.

terdon
  • 242,166
Daniel Kelley
  • 356
  • 1
  • 6