Fg/Bg Command - Linux Manage Process Foreground / Background

August 12, 2013 | By
| Reply More

Linux treats everything as file. Even the processes are present as files under /proc directories. The processes are basically programs in execution. They can also be called jobs. We can run the jobs in background without any intervention from user. The jobs can be run in foreground as current jobs. The bg and fg commands are used for this purpose.

bg command

The processes that have been stopped by some interrupt signal can be continued in background with bg command.

$ nautilus
^Z
[1]+ Stopped nautilus

This process has been stopped with ctrl+z interrupt signal. The stopped processes can be checked with jobs command. But before checking, let us create another job and interrupt it with stop signal.

$ jedit
9:22:28 AM [main] [warning] jEdit: inifPLAF failed to set required l&f
9:22:29 AM [main] [warning] jEdit: inifPLAF failed to set required l&f
^Z
[2]+ Stopped jedit

$ jobs
[1]- Stopped nautilus
[2]+ Stopped jedit

You can see that the status of the jobs is stopped. The plus sign (+) indicates current job. Minus sign (-) refers to previous job. Now, to run the stopped job in background, we use bg command. By defeult, if no argument is given, bg runs the current job in the background.

$ bg
[2]+ jedit &

$ jobs
[1]+ Stopped nautilus
[2]- Running jedit &

bg command can take the job number as argument. Job number is indicated in the square brackets in the output of jobs. In above example, the job number of nautilus is 1. So now we use this job number to run the job in background. % sign is required for indicating the job number.

$ bg %1
[1]+ nautilus &

$ jobs
[1]- Running nautilus &
[2]+ Running jedit &

The bg command can also refer to a job by its name. For example %String refers to a job whose name begins with the specified string and %?String refers to a job whose name contains the specified string. For illustartion, we have two stopped jobs.

$ jobs
[1]+ Stopped nautilus
[2]- Stopped jedit

$ bg %nau
[1]+ nautilus &

$ jobs
[1]- Running nautilus &
[2]+ Stopped jedit

$ bg %?ed
[2]+ jedit &

$ jobs
[1]- Running nautilus &
[2]+ Running jedit &

fg command

The fg command is like bg command except that instead of sending a command in the background, it runs them in foreground and occupies the current terminal and waits for process to exit.

# jobs
[1]- Stopped makewhatis
[2]+ Stopped vi

Without any argument, fg will run the current job in foreground (vi in this case).

# fg
vi

As the command is running in foreground, we don't get back the terminal unill command exits. So, jobs command will now show only one job as vi will exit by now.

# jobs
[1]+ Stopped makewhatis

# fg %1
makewhatis

# jobs

No remaining job. Like bg, %String and %?String work for fg as well.

# jobs
[1]- Stopped makewhatis
[2]+ Stopped vi

# fg %v
vi

# fg %?what
makewhatis

Filed Under : LINUX COMMANDS

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.