0

I want to kill a process and I get its id with:

pgrep -f "python myscript.py"

I would like to call

kill -s SIGINT 

on it, but I can't find any way to do it.

(the command needs to be in one line)

Ambi
  • 513
  • 1
  • 6
  • 18

4 Answers4

4

Try the backtick operator for evaluating a sub-command

kill -s SIGINT `pgrep -f "python myscript.py"`

(untested)

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • 1
    Mr. Spuratic makes a good point about how roundabout this actually is. I was not sitting in front of a Linux system when I wrote it, so stuck with generic *nix-isms rather than details of the particular tools. – Chris Stratton Dec 14 '13 at 22:40
4

Read the man page, pgrep and pkill are the same program. Use pkill to send a signal to one or more processes which you can select in the same way as pgrep.

pkill -INT -f "python myscript.py"

See also this question and answer on unix.se (where this question would be a better fit).

Community
  • 1
  • 1
mr.spuratic
  • 9,767
  • 3
  • 34
  • 24
3

It's generally most convenient to use xargs to pass data from a pipe as arguments to a command that doesn't read data from stdin themselves:

pgrep -f "python myscript.py" | xargs kill -s SIGINT
1

You can also kill a process by name

killall -s SIGINT processname

Sash
  • 4,448
  • 1
  • 17
  • 31