How to manage background and foreground jobs

By default, a process running in the terminal is a process running in the foreground. A process running in foreground takes control over your terminal until it finishes its activity.

A process can also run in background. A background process gets data from the standard input, but it continues its job.

How to run a process in the background:

To run a process in background, insert an ampersand “&” after the process name, when launched from the terminal.

Example: firefox & .

You run a process that needs a lot of time to finish its task in background, in order to keep your terminal free, to use it before your process is finished.

$ gnome-calculator &
[1] 5249
$ vim &
[2] 5264

Note: Sometimes it is usefull to redirect the output of the programs running in background.

I have this alias: alias ffx=’firefox &>/dev/null &’. When I type ffx in my terminal, it starts firefox in the background and redirects the output, so that my terminal is not busy.

The background processes die when you close the terminal. If you want your processes to be independent from the terminal, demonize them with nohup.

How to send foreground processes in background. Or how to suspend processes:

The ampersand trick does not work if the process is already running in foreground. The Ctrl – Z key combination will send your foreground process to background. Actually, Ctrl – Z will suspend the process (pause it).

Example:

$ vim file
[i am inside vim now]
[Ctrl - Z]
[6]+ Stopped vim file

$ du -hs /etc
0 A
244K acpi
4.0K adduser.conf
4.0K adjtime
12K alternatives
[Ctrl - Z]
[9]+ Stopped du -hs *

How to view the background process list

To view all the processes running in the background use jobs:

$ jobs
[2]   Stopped  vim
[3]   Running  firefox &
[4]   Running  gnome-calculator &
[5]-  Stopped  vim
[6]+  Stopped  nano

The number in the square brackets [ ] is the job id. In my job list, I have 2 processes runnig and three suspended. The + sign marks the most current stopped job and the – sign marks the job suspended before the last suspended job (before the job with the + sign)

How to use the bg and fg commands:

fg sends processes to foreground. without any arguments, fg sends the last suspended process back in the foreground.

$ vim a.out
[i am inside vim now]
[Ctrl - Z]
[1]+ Stopped vim a.out
$ fg
[i am inside vim again]

If you have more than one process in background, use fg %job_id to send the process in the foreground.

$ jobs
[1] Stopped vim a.out
[2]- Stopped vim b.out
[3]+ Stopped vim c.out

$ fg %2
[i am inside vim now: b.out file]

To continue (unpause) a suspended process in background use bg %job_id , or simply bg, if the process you want to continue is the last suspended one.

This is usefull when you suspend the an archivation process and need to continue it later, in background, to have your terminal available.

How to kill jobs by their job ids:

You can also kill jobs by their ids, not only by their pids or names:

$ jobs
[1] Stopped vim a.out
[2]- Stopped vim b.out
[3]+ Stopped vim c.out
$ kill -9 %2
[2]- Stopped vim b.out

$ jobs
[1]- Stopped vim a.out
[3]+ Stopped vim c.out

Share and Enjoy

  • Twitter
  • Facebook
  • Google Plus
  • Email
  • RSS
Tagged with: , , , , , , ,
Posted in The Linux and Unix Articles!
One comment on “How to manage background and foreground jobs
  1. deepanshu sharma says:

    this is a very informative article.
    i would like to know can we enter any data int background processes using the shell???

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!