Would the following be the right way to do it in your opinion? Can you suggest a better alternative or, if not, is there something that can be done to make the existing version better?
ps -e | grep skype | cut -d" " -f1 | xargs kill -s term
1 Answer 1
As the comments have pointed out, there exist tools to match processes by name. This is roughly equivalent (better than) what you're trying to do:
pkill skype
It's better because:
- It's shorter and cleaner
- It won't kill itself
By the second point I mean that ps -e | grep skype
will match the grep
process itself too. That won't happen when using pkill
.
Code review
If we wanted to imitate pkill
, I would suggest writing like this:
ps -e | awk '/[s]kype/ { print 1ドル }' | xargs kill
That is:
- Replace two processes with one (
grep
+cut
withawk
) - Use
[s]kype
instead ofskype
is a common trick to make theawk
itself not match - Drop
-s term
fromkill
, asTERM
is the default signal anyway
killall
to take care of this. \$\endgroup\$/usr/bin/pkill
is in the same procps package as/bin/ps
, but/usr/bin/killall
is in a separate psmisc package. Also, fun fact: on Solaris,killall
actually tries to kill everything that it possibly can, regardless of command-line parameters! For both reasons, I recommendpkill
overkillall
. \$\endgroup\$