bg command in Linux is used to move a suspended or stopped job to the background, allowing it to continue running without interaction. It is often used in conjunction with the
fg commands to manage background processes within a shell session.
Here’s an overview of how to use the
- Suspending a Job:
To suspend a running job and transfer it to the background, you can use the
Ctrl + Zkeyboard shortcut. This will pause the currently running foreground job and return control to the shell.
- Viewing Jobs:
After suspending a job, you can use the
jobscommand to display a list of jobs associated with the current shell session. Each job will have an associated job ID, which you can use to reference the job in subsequent commands.
- Moving a Job to the Background:
To move a suspended or stopped job to the background, you can use the
bgcommand followed by the job ID. For example:
This command moves the job with ID 1 to the background.
- Confirming Job Status:
After using the
bgcommand, you will see a confirmation message indicating the job’s status and the new process ID assigned to it.
- Checking Background Job Completion:
To check the completion status of a background job, you can use the
jobscommand again to view the list of jobs and their statuses.
- Redirecting Output:
By default, the output of background jobs is redirected to the terminal’s standard output. However, if you want to redirect the output to a file, you can use shell redirection. For example:
bg %1 > output.txt
This command redirects the output of the job with ID 1 to the file “output.txt” instead of displaying it in the terminal.
bg command is particularly useful when you have a suspended job and want to move it to the background so that it can continue running independently. It allows you to free up the terminal and continue working on other tasks.
Remember that the
bg command operates on suspended or stopped jobs; it cannot be used to directly start a new job in the background.
For more information about the
bg command and its options, you can refer to the manual page by typing
man bg in your terminal.