2
\$\begingroup\$

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
asked Jul 19, 2016 at 22:17
\$\endgroup\$
2
  • \$\begingroup\$ I would install Linux-style killall to take care of this. \$\endgroup\$ Commented Jul 19, 2016 at 22:21
  • 1
    \$\begingroup\$ @chicks In Linux, /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 recommend pkill over killall. \$\endgroup\$ Commented Jul 19, 2016 at 22:30

1 Answer 1

1
\$\begingroup\$

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:

  1. It's shorter and cleaner
  2. 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 with awk)
  • Use [s]kype instead of skype is a common trick to make the awk itself not match
  • Drop -s term from kill, as TERM is the default signal anyway
answered Aug 1, 2016 at 20:18
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.