8

The command

ping <dest> &

causes ping to go to the background. It still prints output to the terminal, however. Ctrl-C does not stop it, only introduces a new prompt. How to stop it from the terminal?

New contributor
Vineet is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • In addition to the answers, there is a way to naturally limit ping's iterations with a count — -c N — where N is a positive number. – l0b0 Mar 15 at 18:07
  • 1
    @l0b0 Yes true, but I faced this particular problem, and it's actually related to any program where we might not know the execution time, or where it might not even be defined. So I mean to ask how to stop the process. – Vineet Mar 15 at 18:20
  • Nitpick: It's not a URL. – Roger Lipscombe Mar 15 at 18:43
  • @RogerLipscombe What do you call it, other than destination? Host is a little unsure, considering that a single computer may host many domains – Vineet Mar 15 at 18:48
  • It's a hostname (or an IP address). "a single computer may host many domains" -- not relevant to ping. – Roger Lipscombe Mar 15 at 19:14
20

First enter fg into same terminal that your ping command is running (it brings the process into the foreground), then press Ctrl+c to stop the process.

enter image description here

  • This works. I also realized that fg works if we have multiple background processes, by calling them to the foreground one by one. – Vineet Mar 15 at 14:22
  • Yep, that's how it works, you can use jobs to get a list of process that are running in background. – Ravexina Mar 15 at 14:23
  • How did you make that image loop? – phillipsk Mar 16 at 18:25
  • 2
    @phillipsk It's GIF, that's how GIF works ... – Ravexina Mar 16 at 18:31
12

If it is your one and only background job you can kill it with kill %1. If not sure you can list all your background jobs with jobs and use kill %<n> where you replace n by the number of your ping job.

  • 4
    Also, I found out that n matches the number that is print out in square brackets when running the command. – Vineet Mar 15 at 14:31
5

When you send a process to the background, whether by using ctrl-z or by & at the end of the command, you get an output in the following format: [index] process-id. If you send multiple processes to the background, the index will keep incrementing every time.

For example:

$ sleep 100 &
[1] 41608
$ sleep 101 &
[2] 41609
$ sleep 102 &
[3] 41610
$ sleep 103 &
[4] 41611
$ sleep 104 &
[5] 41612
$ sleep 105 &
[6] 41613
$ sleep 106 &
[7] 41614

In order to stop a specific one, you can either use kill <process-id> or use fg <index> followed by ctrl-c

Example using the previous output:

$ kill 41614

or

$ fg 7
sleep 106
^C
4

Launch a new tab of terminal, run:

$ pgrep ping
  2564

Then kill the pid using kill command:

$ kill 2564
  • I used kill in the same terminal too (though the output made it difficult to use). However, I was wondering what to do if I couldn't go far back up in the terminal to see the process id printed, so pgrep has helped, additionally. – Vineet Mar 15 at 14:24
  • 2
    When you use pgrep to search processes by name, you can also use pkill to kill them by name. No need to type/copy the process id and use kill. – Byte Commander Mar 15 at 14:29
  • This is useful to know, but for this question it's a bit of overkill. :-p – tudor Mar 18 at 1:45
0

When you are root, it's simply killall ping.

0

Slightly different approach towards a continuous ping is to use -c option and enter the number of time you want it to run, that way it will stop itself after the desired count i.e. below ping will stop after 100 pings

ping -c 100 192.168.1.1 &

Your Answer

Vineet is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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