20 June, 2011

Background and Foreground process manipulation in Linux

Most crucial aspect of modern multitasking operating systems is to understand difference between  background and foreground processes. Process is just a technical way to call a running program. When you are interacting in some way with a process (using interface) or watching its output in a terminal, you call that process the foreground process. All other processes on your computer are either suspended processes (paused at the moment) or background processes (still running, but you can't interact with them).

Here is an example of a foreground process in terminal using vi command as text editor.
Notice that a prompt is not available to run another command. In fact, terminal will interpret keyboard commands as user interface for vi editor. To restore a BASH prompt, you will need to terminate (close) foreground process. You can type :q! and then hit enter on the keyboard to quit vi. Also, you can use CTRL + C combination to terminate ANY foreground process.
If you want to suspend foreground process, then use CTRL + Z combination. This is extremely useful if you are tailing output of some log file and want to suspend it to see something else so you can later return to it.
Here is another example:
Here I firstly run the vi editor and later, suspended it. I tailed some log file after (using tail -f command) and suspended it also. Running jobs -l command allows you to see all background and foreground processes for your terminal session (if you want to see all running processes in the system, use ps -ef command).
If you want to bring one of these processes to the foreground, use fg command with a job number as its argument (you can find job number as a number in square brackets in jobs -l output). To bring vi process, use fg 1. To bring tail process, use fg 2 instead. Remember, to suspend any process use CTRL+Z keyboard combination. If you are curious, you can terminate any of these processes by typing kill -9 command with a process group ID as its argument (that number is next to job number). To terminate vi, in given terminal session, use kill -9 16381, or to terminate tail you use kill -9 16505. Keep in mind that process group ID changes from terminal to terminal, so your process group ID, will be 99.97% different.
To learn more about jobs command, read its manual pages: man jobs, or check this page.

What about background processes?
There are time when you need to run certain commands in background (to run parallel with process that called it). Practical example would be to run some web server in background (tomcat, Apache http...), while still being able to use prompt. If web server is suspended, then it wont be able to receive incoming http requests.
Example that we will try here: you are in linux command line and you want to run some GUI program to edit textual files, but you still want to have BASH prompt (to bring other files in editor for example). If you run gedit and type CTRL + Z in BASH terminal (you need to have focus on terminal to do this), you will notice that gedit will stop responding on inputs (window will turn grey).
You can turn it back to foreground using fg command, but this time we will make it go in parallel with terminal by sending it to background. Use bg command followed by a job number (remember, you can get this by using jobs -l command). To send gedit to background, use fg 1 command, and you can then use both gedit and terminal.
There is a quicker way to send process to background when you are actually calling its command. Simply, use & (ampersand) at the end of any of your command to send process directly to background. To run gedit as background process, type: gedit & and you are done. Ampersand argument is extremely helpful in these situations.

Let's recapitulate at the end:
  • To suspend foreground process, use CTRL+Z combination
  • To terminate foreground process, use CTRL+C combination
  • To see list of all processes in currently active terminal session, use jobs -l. (use ps -ef to see all processes in system).
  • To send process to foreground, use fg command with a job number (number in square brackets in the jobs -l command output)
  • To send process to background, use bg command with a job number (number in square brackets in the jobs -l command output)
  • To terminate any process in system, use kill -9 command with a process group ID (number following square brackets in jobs -l command output)
  • To run process immediately in background, add & (ampersand) at the end of command (like gedit &).
Understanding what background and foreground processes are, is very important if you use terminal a lot. For  a desktop environment it may not be so important, since a desktop manager will change processes for you. Still you will surely use terminal even in a desktop environment, so these are very important basics of Linux...

1 comment: