6

I'd like to kill a process/script with a simple command using. At the moment I do the following

ps -ef | grep myscriptname
kill 123456

But is there a way to maybe combine the 2 command together so I don't need to look and manually write the pid, something like this kill grep myscriptname?

Edito
  • 3,030
  • 13
  • 35
  • 67

6 Answers6

16

You want pkill:

pkill myscriptname

On some systems there is a similar tool called killall, but be careful because on Solaris it really does kill everything!

Note that there is also pgrep which you can use to replace your ps | grep pipeline:

pgrep myscriptname

It prints the PID for you, and nothing else.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
4

Another alternative is using the pidof command:

kill $(pidof processname)
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
Mark
  • 56
  • 2
1

you can try this simple trick pkill -f "my_sript_filename"

Touqeer
  • 583
  • 1
  • 4
  • 19
1

I use kill $(pgrep <program name>), it works.

Yan Wen
  • 39
  • 1
  • 3
-1

Another alternative, pgrep with xargs

ps aux | pgrep gitlab | xargs kill

Oswaldo Ferreira
  • 1,339
  • 16
  • 15
-2

An alternative is piping to the xargs command:

ps -ef | grep myscriptname | xargs kill

http://man7.org/linux/man-pages/man1/xargs.1.html

runningviolent
  • 317
  • 3
  • 13
  • 1
    that causes too much killing. i think you meant: `pgrep myscriptname | xargs kill` – webb Apr 22 '16 at 05:19