it's best and safest to use pgrep -f
with kill
, or just pkill -f
, grep
ing ps
's output can go wrong.
Unlike using ps | grep
with which you need to filter out the grep line by adding | grep -v
or using pattern tricks, pgrep
just won't pick itself by design.
Moreover, should your pattern appear in ps
's UID
/USER
, SDATE
/START
or any other column, you'll get unwanted processes in the output and kill them, pgrep
+pkill
don't suffer from this flaw.
also I found that killall -r
/ -regexp
didn't work with my regular expression.
pkill -f "^python3 path/to/my_script$"