Edito
Edito

Reputation: 3747

How to kill a process with 'kill' combined with 'grep'

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?

Upvotes: 6

Views: 20813

Answers (6)

Yan Wen
Yan Wen

Reputation: 49

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

Upvotes: 2

Touqeer
Touqeer

Reputation: 656

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

Upvotes: 1

Oswaldo Ferreira
Oswaldo Ferreira

Reputation: 1339

Another alternative, pgrep with xargs

ps aux | pgrep gitlab | xargs kill

Upvotes: -1

Mark
Mark

Reputation: 56

Another alternative is using the pidof command:

kill $(pidof processname)

Upvotes: 4

runningviolent
runningviolent

Reputation: 317

An alternative is piping to the xargs command:

ps -ef | grep myscriptname | xargs kill

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

Upvotes: -2

John Zwinck
John Zwinck

Reputation: 249303

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.

Upvotes: 16

Related Questions