Ubuntu – How to return to an application in the background

bash

I know that I can run an application in the background by putting an ampersand (&) at the end of the command. For instance:

$ rtorrent &
[1] 4618

starts rtorrent in the background.

I'd like to return to the backgrounded rtorrent to check whether my downloads are completed. How can I do that?

Best Answer

Run fg to put the job back onto the foreground, i.e. give it back control of the terminal. If there are multiple background jobs, run jobs to see a list and fg %1, fg %2, etc., to select which job to put back to the foreground. See the Wikipedia article on job control for more information.

If a program is running on the foreground, press Ctrl+Z to suspend it. When you do this, you'll get a shell prompt. Run bg to make the program keep running, but in the background.

If a background job terminates, the shell will print a notification the next time it displays a prompt. If a background job requires input from the terminal, it will automatically be suspended (“stopped”); fg (or fg %42 if necessary) resumes it.

If you want to trigger a more visible notification when the program finishes, you can run something like

fg; play bell.ogg & xmessage "torrents downloaded"

The fg command returns when the program that it resumes finishes, so whatever you told the shell to run after fg will run when the program finishes. Note that if you press Ctrl+Z to put the job back into the background, then the shell stops waiting and runs the following commands immediately.