1

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

ping -c 70 74.125.228.71

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 74.125.228.71 (74.125.228.71) 56(84) bytes of data.
64 bytes from 74.125.228.71: icmp_seq=1 ttl=122 time=256 ms
64 bytes from 74.125.228.71: icmp_seq=2 ttl=122 time=244 ms
64 bytes from 74.125.228.71: icmp_seq=3 ttl=122 time=250 ms
64 bytes from 74.125.228.71: icmp_seq=4 ttl=122 time=254 ms
64 bytes from 74.125.228.71: 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?

slm
  • 369,824
sureshd
  • 111

4 Answers4

2

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.

  • 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). –  Jun 11 '13 at 09: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. – Has QUIT--Anony-Mousse Jun 11 '13 at 09: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
1

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");

Do:

run("set -m; trap 'kill %' TERM; your-script its args & wait")
0

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

rakib_
  • 101
0

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
Lesmana
  • 27,439