23

If I do ping google.com &, the process goes into the background and keeps showing me the ping details. I can still use terminal with other commands, but the output from ping and output from other commands get mixed up (which is not a problem), but I can't terminate this ping process at all.

For that I want to bring it to the foreground and then press ctrl + c or something. I tried using fg, but it didn't work either. So how do I make ping google.com & come back into foreground after I hit return key at the end of my ping command?

Zanna
  • 72,312
posixKing
  • 1,143

1 Answers1

40

fg is the correct command to foreground a job, but if there is more than one job running you should specify the job number. If you type only fg, the last backgrounded job (considered the current job by the shell) will be brought to foreground.

Get the current jobs:

$ jobs
[1]+     ping google.com &

Foreground the job by specifying the number:

fg %1

The command may be broken up by output from the running process, but it will still work, you can ignore the interruption.

Then hit ctrl+c to interrupt and stop the process

As mentioned in a comment, you can also send signals by job number, for example kill %1

For completeness, if you didn't use the & at the time of calling the process, you can send it to the background by first freezing with ctrl+z and then typing bg

Zanna
  • 72,312