I have some processes that I am running both locally and within docker containers on the same host. Since I run multiple processes with the same name, I would like to make an alias/function that would allow me to kill all of them that are running on the host, but ignore the ones that are running in docker.
The command that I am using now has this form:
kill $(ps aux | grep 'process_name' | awk '{print $2}') I noticed that this command kills the processes with the same name in all of the running containers which makes them crash. How can I prevent this?
This is how the process on the host machine looks like (as a result of ps aux):
hamzam 9109 0.9 0.0 510912 24732 ? Ssl 14:21 0:00 python /opt/scripts/ex_script __name:=process_name __log:=/home/hamzam/.logs/0286c734-71fd-11e7-9dc4-901b0ed728a7/process_name-3.log And this is the process which is run in one of the containers:
hamzam 12168 2.3 0.0 472708 64312 ? Ssl 14:22 0:00 python /opt/scripts/ex_script __name:=process_name __log:=/home/logs/0/126e36dc-71fd-11e7-bd98-0242ac110003/process_name-3.log
ps aux. Show at least one you would want to kill and one you would want to keep. Also, as a general rule, you don't useps | grepfor this sort of thing. That's whatpkillis for.ps aux. You will need to use the parent PID, probably. Could you show us the output ofpstree $pidwhere$pidis the PID of each of the two (host and container) python processes?\python───5*[{python}]. Also, I don't get the same results when I dops auxandpkill. When I dopkill 'process_name'nothing happens actually, only when I dopkill python, but I don't want to kill all the python processes. EDIT:pkill -fworks. Sorry for so many edits!pkillhas--ns pidand--nslist name...options which you can use to limit the slaughter to a specific namespace. e.g. using pkill with--ns $$from sh/bash/etc should kill only matching processes in the same namespace as your shell (all your docker containers will be in different namespaces).