Find and kill a process in one line using bash and regex
How can I extract the process id automatically and kill it in the same line?
bash, you should be able to do:
Details on its workings are as follows:
and you can see it terminating all the sleepers.
grep '[p]ython csp_build.py' bit in a bit more detail:
When you do
sleep 3600 & followed by
ps -ef | grep sleep, you tend to get two processes with
sleep in it, the
sleep 3600 and the
grep sleep (because they both have
sleep in them, that's not rocket science).
ps -ef | grep '[s]leep' won't create a process with
sleep in it, it instead creates
grep '[s]leep' and here's the tricky bit: the
grep doesn't find it because it's looking for the regular expression "any character from the character class
[s] (which is
s) followed by
In other words, it's looking for
sleep but the grep process is
grep '[s]leep' which doesn't have
sleep in it.
When I was shown this (by someone here on SO), I immediately started using it because