15

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?

  • 2
    It seems like the problem you actually want to solve is killing a background job; there's no need to bring it into the foreground. Use jobs to find the Job ID jid of the background process, and then just do kill %jid to kill it. – gardenhead Sep 2 '16 at 14:26
29

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

  • 1
    Simply fg without an argument seems to work too, at least when only having one background job. – Cedric Reichenbach Sep 2 '16 at 13:15
  • 1
    @CedricReichenbach edited! – Zanna Sep 2 '16 at 13:29
-2

To answer the question asked, namely How to send output of a process to background and foreground?, the tee command will do:

In computing, tee is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. https://en.wikipedia.org/wiki/Tee_(command)

  • 4
    You're misunderstanding something here... – Byte Commander Sep 3 '16 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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