verystrongjoe
verystrongjoe

Reputation: 3911

Using a shell script to automatically kill processes

I am looking for a command that will find PIDs such as:

ps -ef | grep com.sds.afi.rte.cosmos-1.0.0.jar
cosmos    4690  4689  3 14:27 pts/8    00:00:06 java -Dlog4j.debug -Dlog4j.configuration=file:/data/cosmos/sim/bin/log4j.xml -jar com.sds.afi.rte.cosmos-1.0.0.jar
cosmos    5484  5482  0 14:30 pts/11   00:00:00 grep com.sds.afi.rte.cosmos-1.0.0.jar

and then kill these processes using:

kill -9 pid

How can I make a shell script that will do all the above automatically ?

Upvotes: 2

Views: 34109

Answers (7)

Andrew  Haraldstad
Andrew Haraldstad

Reputation: 1

I made a .sh to restart my Wifi after putting it in monitor mode with the following lines.

#Display PID

echo "Killing network PID'S"

ps aux | grep wpa_supplicant | awk '{print $2}' | xargs kill



ps aux | grep NetworkManager | awk '{print $2}' | xargs kill

#Restart NetworkManager && wpa_supplicant

echo "Restarting NetworkManager && wpa_supplicant"

service NetworkManager restart && service wpa_supplicant restart

Upvotes: 0

Suraj Branwal
Suraj Branwal

Reputation: 29

You can include below command in your shell script which will kill the process id for "com.sds.afi.rte.cosmos-1.0.0.jar"

kill -9 ps aux | grep com.sds.afi.rte.cosmos-1.0.0.jar | grep -v grep | awk '{print \$2}'

make sure you use inverted (``) comma for pid search as used above.

if you want to execute the above command from expect command then you can use below:

expect "$"

send "kill -9 ps aux | grep com.sds.afi.rte.cosmos-1.0.0.jar | grep -v grep | awk '{print \$2}'\r"

Cheers, Suraj

Upvotes: 0

ErJab
ErJab

Reputation: 6375

killall java

Or more generically:

killall <processname>

Sometimes I have processes with the same name, but different command line arguments. To kill such processes or any arbitrary process without having to type in ps and then kill pid, I do this:

ps aux | grep <something> | awk '{print $2}' | xargs kill

where

  • <something> is any phrase that you want to search for in the ps aux command's output.
  • awk '{print $2}' will filter out only the 2nd column, which is a list of PIDs
  • kill will be called on each of those PIDs.

Edit: As tripleee points out, it is a bad idea to lash out kill -9 on a process unless absolutely needed. So removed the -9 part from the above command

Upvotes: 13

Andreas Covidiot
Andreas Covidiot

Reputation: 4755

I find the pkill/pgrep commands as mentioned by nikeairj the best choice if available. I also used the following which may also work in Linux/Unix OSes without awk or other possibly unavailable commands (I remember not beeing able to use awk in some AIX or HP-UX environments)

# subsitute myMatch with your process cmdline match, e.g. "firefox", "firefox -P"
# or anything output by "ps -ef"
ps -ef|grep myMatch|grep -v grep|sed -e "s/^[^0-9]\+\([0-9]\+\)\s.\+$/\1/"|xargs kill "{}"

Upvotes: 0

Sjors
Sjors

Reputation: 1

In addition to the solution ErJab provided, I created a shell script killall to mimic the 'killall' behaviour:

#!/bin/sh
ps | grep $1 | awk '{print $1}' | xargs kill -9

Which I placed in the /bin directory (after chmod +x, of course)

Upvotes: 0

nikeairj
nikeairj

Reputation: 173

I think pkill -9 java is the easiest way. pkill will use grep to find a matching process name.

See the manual page: http://linux.die.net/man/1/pkill

Upvotes: 3

Mark Bramnik
Mark Bramnik

Reputation: 42481

Try this:

kill -9 `pidof java`

Upvotes: 1

Related Questions