Bg,Fg,CtrlZ Unix command to Manage Unix Background Jobs

Last updated on August 19th, 2016 at 06:01 pm

There are several commands which allow you to manage the processes that belong to you. Off course You cannot do anything with processes belonging to other users unless you are root user. Here is the detail on Bg, Fg, CtrlZ  Unix command  to Manage Unix Background Jobs with Examples

Some definition

What is foreground process

When you enter a command at the shell prompt, your shell forks a child process in which to execute the command. This process is called a foreground process.

What is background process

You can send the foreground process to the background process so that you can execute other command on the prompt.


How to cancel the foreground process that is currently running

To cancel a foreground process that is currently running enter : Ctrl-c

$ find /tmp -mtime -1 > /tmp/1.txt

$ [CTRL-C]

[2]+ killed                 find /tmp -mtime -1 > /tmp/1.txt

To suspend a foreground process

Enter : Ctrl-z
If you are using the Bourne shell you will then have to kill the process. Other shells provide you with a facility to restart a process in the foreground or as a background job.

$ find /tmp -name “*123* > /tmp/1.txt

$ [CTRL-Z]

[2]+ stopped                 find /tmp -name “*123* > /tmp/1.txt

To run a foreground process as a background job:
1. Suspend the foreground process.
2. Enter the bg (background) command to move the process into the background.


$ [CTRL-Z]

[2]+ stopped                 find_old_logs.ksh

$ bg

To run a process in the background as a job 

We need carry on working add an & (ampersand) at the end of the command line.

The shell forks a child process to run the command and displays the job number ([n]) and the PID (Process IDentifier) number. The shell prompt returns and you can enter further commands. Redirect the standard output for a command that is being run in the background to a file. This prevents the output from the command appearing on your screen and interrupting your current work

For example:

find /tmp -name “*/log” -print > log.txt &

[1] 123

How to list  the background jobs running

You can use the command jobs [-l] to list the background jobs.

it displays the process number.

$ jobs

[111] + Running    > x.log

[112] – Running    > y.log

[113]   Running   > z.log

To bring a specific background job into the foreground

fg job_number
If there is only one job running in the background just enter the fg command.

$ jobs

[1] + Running    > x.log

[2] – Running    > y.log

[3]   Running    > z.log

$ fg 3

$ > z.log

This starts a process to print a file as a background job. The user then checks the current active jobs and brings the print job – job number three (3) – into the foreground.

To monitor the background jobs and Kill them if required

ps [-option] ps used without options this produces a list of all the processes owned by you and associated with your terminal.
kill [-signal] (PID) Use the ps command if you need to find the PID of a process. Always try to kill a process with a simple kill command.
kill (PID) This is the cleanest way to kill a process and has the same effect as canceling a process.
kill -1 (PID) This tells the process to hang-up just as though you were logging out. The system will attempt to kill any child processes.
kill -9 (PID) This will stop the process dead in its tracks but it may leave any child processes still running.

I hope you like the explanation for  Bg,Fg,CtrlZ  Unix command. Please do comments

Related articles

split ,tr and tee unix command with examples

Practical Unix commands with example

Functions in Shell Script with some practical examples

Writing loop statement in Unix shell scripting with examples


Leave a Reply