11 fc, bg and jobs commands you should know

296

Some times we require to control jobs/commands on how they are running ie either foreground or background on the screen. Commands which run longer duration and do not require any manual intervention are sent to background so that we can run other commands in foreground. In this post we will see how to manage process/commands to run in background or foreground by using some inbuilt commands like fg and bg.

Example 1: Run a command on foreground.

 

sleep 1000

Sleep is an excellent command which makes our screen to sleep. The above command will block our terminal for 1000 seconds or 16+ minutes.

Example 2: How can we send that to background? As we already ran that command in foreground we have to do two steps to send it back ground.

Step 1: Press ctrl+z which will stop the process running on foreground.

Note: This will not send the process background by default, it just suspends it from running on foreground.

surendra@linuxnix ~/$ sleep 100 ^Z [1]+ Stopped sleep 100

Check with jobs commands if the stopped command is running or not, to check what ctrl+z did to our process.

surendra@linuxnix ~/$ jobs [1]+ Stopped sleep 100

Step 2: Now we conformed it is not running in background, we have to execute bg command to send this process background.

surendra@linuxnix ~/$ bg [1]+ sleep 100 &

Check if the process which we sent background is running or not using jobs commands again and see the status changed from stopped to running.

surendra@linuxnix ~/$ jobs [1]+ Running sleep 100 &

Example 3: Suppose we want to send second process which is stopped by using ctrl+z, then use below command.

bg %number

Example

surendra@linuxnix ~/$ sleep 300 ^Z [1]+ Stopped sleep 300

surendra@linuxnix ~/$ sleep 200 ^Z [2]+ Stopped sleep 200

surendra@linuxnix ~/$ jobs [1]- Stopped sleep 300 [2]+ Stopped sleep 200

surendra@linuxnix ~/$ bg %2 [2]+ sleep 200 &

surendra@linuxnix ~/$ jobs [1]+ Stopped sleep 300 [2]- Running sleep 200 &

If you observe first process is still in stopped condition and where as second process is running in back ground.

Example 4: Send process directly to background for running with out above two steps. Use & at the add of the command.

surendra@linuxnix ~/$ jobs
surendra@linuxnix ~/$ sleep 200 & [1] 31951
surendra@linuxnix ~/$ jobs [1]+ Running sleep 200 &

Example 5: How about bringing first process from suspension status to foreground running state?

fg

Note : By default fg brings first back ground running process to foreground.

Example 6: How about bringing a specific running process to foreground? Use %number as we did for bg command.

fg %2

Example 7: We can even kill a background running process with out knowing what is the PIDof it. To kill first process use below command.

kill %1

Example 8: How about killing 3rd background running process, yes you guessed it right. It iskill %3

kill %3

Example 9: How about list all the background running process with details?

surendra@linuxnix ~/$ jobs [1]+ Running sleep 200 &

Example 10: Just list the PID’s for all background running processes.

surendra@linuxnix ~/$ jobs -p 31951

Example 11: When we send process to background by using & or bg, Some times we can not exit from terminal until it is completed. In those cases, if you want to run a process in background we can using any of below commands.

nohup
screen
at

We will see about screen and at command in our coming posts.