Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script file pingIP.sh which just pings the given IP address,

ping -c 70

The problem is when I try to stop the executed script in the middle its not stopping it properly.

Using linux cmd ps -ef|grep pingIP |grep -v 'grep pingIP' I'm able to get the process id, and killing it using cmd kill -9 processid.

For example, executing script file

PING ( 56(84) bytes of data.
64 bytes from icmp_seq=1 ttl=122 time=256 ms
64 bytes from icmp_seq=2 ttl=122 time=244 ms
64 bytes from icmp_seq=3 ttl=122 time=250 ms
64 bytes from icmp_seq=4 ttl=122 time=254 ms
64 bytes from icmp_seq=5 ttl=122 time=243 ms

Executing the kill command now just stops the script execution but ping response is continuing until specified ping count is reached.

Any suggestions to avoid the ping execution even after stopping the execution of script file?

share|improve this question

migrated from stackoverflow.com Jun 11 '13 at 11:25

This question came from our site for professional and enthusiast programmers.

Ctrl+c in terminal didn't help? – Dilawar Jun 11 '13 at 9:12
Ctrl+c will work in shell.. since im doing the above process using Java, I need linux cmds to do it – sraina Jun 11 '13 at 9:21
Use Process.destroy() if you are doing this from Java. – Joni Jun 11 '13 at 9:59
do not kill -9 – lesmana Jun 11 '13 at 12:10

If you want to kill the shell process and any other process it has spawned, best is probably to start a new process group (setpgid() in the child after the fork() before running the execve() on the script), and kill the process group with kill(-pid) where pid is the return value of fork() in the parent.

If calling setpgid() is not an option, you can still do it like:

Instead of:

run("your-script its args");


run("set -m; trap 'kill %' TERM; your-script its args & wait")
share|improve this answer

Usually, one would Ctrl+C on the invoking shell / kill the process from the parent process by sending a signal via Process.destroy() (Java).

A neat tool for such tasks is pkill, which can kill processes by a pattern, e.g.

pkill ping

Note that a regular user may not kill another users processes, so make sure you are logged in on the right computer with the right user id.

Bonus tip: use pgrep -fl ping to list matching processes, to make sure you want to kill all of them - because running as root, pkill ssh will also kill your sshd and may lock you out. So in particular when running as root - which you should avoid anyway - double check with pgrep.

share|improve this answer
pkill is dangerous sometime. For instance, if you want a to kill a ssh connection, pkill ssh will kill sshd, and kick you out of your server (if you were logged as root). – Uman Jun 11 '13 at 9:14
Not so much as user, with a safe pattern such as ping when you don't know your way around anyway. ;-) But yes, it's best to test with pgrep first. – Anony-Mousse Jun 11 '13 at 9:15
pkill ping kills any process whose process name contains ping (more precisely that match the ping regular expression). pkill -x ping will kill the processes whose process name is ping, but still that kills all the ping processes, not just the one you started. – Stéphane Chazelas Jun 11 '13 at 14:15

Use killall. For ex. "killall scriptname". killall kills a process by name. Additionally you can also issue signal like. SIGKILL, SIGSTOP etc.

share|improve this answer
On some systems it really kills all processes. – jordanm Jun 11 '13 at 14:24

Get process ID as you've written:

ps -ef|grep pingIP |grep -v 'grep pingIP'

and then kill children process using

pkill -P process-id
share|improve this answer
Why this hack with grep -v grep? OUCH. man pkill! (Much smarter solutions have been posted hours ago!) – Anony-Mousse Jun 11 '13 at 12:37
Agree but it doesn't matter. I've just copied it from 'sraina' question. pkill -P is the essence. – Krzysztof Księżyk Jun 12 '13 at 10:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.