Bash Script to kill some instances

multisites used Ask the Experts™

I need a little Bash Script, to be executed as a cron job, say, each 30 minutes and just do:

1) Issue the command (lsof -i | grep irc) to see if there's any IRC connection established.

2) If there's not it will do nothing. If there is any, it shall list one or more lines like this:

named 6059 named 25u IPv4 20094  TCP (LISTEN)
named 3219 named 25u IPv4 20094  TCP -> nnn.nnn.nnn.nnn (ESTABLISHED)

3) In this case, it would kill all the instances listed, issuing, as in this example:

kill 6059
kill 3219

Thanks a lot.

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try this:

kill `lsof -i | grep irc  | awk '{print $2}'`

Open in new window

Even simpler:

kill `lsof -i | awk '/irc/ {print $2}'`

Open in new window


I tried but it answered as below, probably due to a sintax error when issuing the "kill" command:

kill: usage: kill [-s sigspec | -n signum | -sigspec] [pid | job]... or kill -l [sigspec]

Maybe the Script must separate just the PID number from the "lsof" command output before issuing the "kill" command.

Have you tried manually killing the process, like kill 6059? If it doesn't work, try kill -9 6059

Let's debug:

Check whether this is giving proper output:

lsof -i

Then check this to see if the pids are filtered properly:

lsof -i | awk '/irc/ {print $2}'

Then run the kill command


Yes, it worked. Thanks a lot.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial