run process foreground

How to Bring Background Process to Foreground in Linux

Sometimes you may need to bring background processes to foreground in Linux. You can easily do this using fg command in Linux. In this article, we will look at how to bring a process to foreground.


How to Bring Background Process to Foreground in Linux

Let us first push some processes to background for our example. The easiest way to push a process to background is using & operator.

$ vi &
$ crontab &

Now if you want to list all jobs running in background use jobs command. It will display job id in square bracket, followed by its status (running, stopped, etc.), followed by the actual command for the process. It also displays + and – sign next to job ids to denote the last and second last jobs.

$ jobs
[1]-  Running                 vi &
[2]+  Running                 crontab &

You can easily bring these processes using fg command. Now we will look at different use cases for fg (foreground) command.


1. Bring last background process to foreground

If you run fg command without any options or arguments then it will bring back the last process that was pushed to background.

$ fg

In our case, the above command will bring crontab command to terminal.


2. Bring specific process to foreground

If you want to bring a specific process to foreground, then mention its job id after fg command. Here is an example to bring the first process, that is, job id 1, to foreground.

$ fg 1

The above command will bring vi editor to terminal.

Also read:

LS File Size in kb, Mb
How to Get User Input in Shell Script
Shell Script to get CPU and Memory utilization
How to Check SSD Health in Linux
How to Set Default Gateway in Linux

Leave a Reply

Your email address will not be published.