The fg
command in Linux is used to bring a background job to the foreground, allowing you to interact with it directly. It is often used in conjunction with the jobs
command to manage background processes within a shell session.
Here’s an overview of how to use the fg
command:
- Starting a Background Job:
To start a job in the background, you can append the&
symbol at the end of a command. For example:
sleep 60 &
This command starts the sleep
command in the background, causing it to run for 60 seconds without blocking the terminal.
- Viewing Background Jobs:
Once you have background jobs running, you can use thejobs
command to view their status. Typejobs
in your terminal, and it will display a list of background jobs associated with the current shell session. Each job will have an associated job ID. - Bringing a Job to the Foreground:
To bring a background job to the foreground and resume its execution, you can use thefg
command followed by the job ID. For example:
fg %1
This command brings the job with ID 1 to the foreground.
- Job Execution:
Once a job is in the foreground, it will resume execution, and its output will be displayed in the terminal. You can interact with the job directly, providing input if necessary. - Suspending a Foreground Job:
To suspend a running foreground job and transfer it to the background, you can use theCtrl + Z
keyboard shortcut. This will pause the job and return control to the shell. - Bringing a Suspended Job back to the Foreground:
If you have a suspended job and want to bring it back to the foreground, you can use thefg
command followed by the job ID. For example:
fg %1
This command brings the suspended job with ID 1 back to the foreground.
The fg
command is useful when you have a background job that requires user interaction or when you want to monitor its progress directly.
Remember that the fg
command operates on background or suspended jobs, allowing you to bring them to the foreground. It cannot be used to directly start a new job or send a running foreground job to the background.
For more information about the fg
command and its options, you can refer to the manual page by typing man fg
in your terminal.