Full Version: Unix/Linux background processing
Dream.In.Code > Programming Tutorials > Linux Tutorials
no2pencil
In Unix, you can run a process in the background, independently of the shell. The key benefit is, this will allow you to leave the terminal available for other work. For each program that you send into the background, you can call it back to the foreground to re-enter interaction with the process.

To launch a process into the background, you can simply add a space and an ampersand after your command.

CODE

$  top &
[1] 26452


This forces your program to run in the background. The shell will tell you that one process has started and show its process identification (PID) number.

You can use the shell to check on the process.

CODE

$  ps aux | grep 26452
user    26452  0.0  0.0   2072   660 pts/2    T    09:06   0:00 top


Also, you can bring a background process to the foreground.

CODE

$  fg 26452


If you no longer wish to leave the process running, you can also kill the process while it is in the background.

CODE

$  kill -9 26452


Or, you can force it to restart.

CODE

$  kill -HUP 26452


Here is a list of the available signals, from wiki http://en.wikipedia.org/wiki/Signal_%28computing%29
QUOTE

SIGABRT - process aborted
SIGALRM - signal raised by alarm
SIGBUS - bus error: "access to undefined portion of memory object"
SIGCHLD - child process terminated, stopped (*or continued)
SIGCONT - continue if stopped
SIGFPE - floating point exception: "erroneous arithmetic operation"
SIGHUP - hangup
SIGILL - illegal instruction
SIGINT - interrupt
SIGKILL - kill
SIGPIPE - write to pipe with no one reading
SIGQUIT - quit
SIGSEGV - segmentation violation
SIGSTOP - stop executing temporarily
SIGTERM - termination
SIGTSTP - terminal stop signal
SIGTTIN - background process attempting to read ("in")
SIGTTOU - background process attempting to write ("out")
SIGUSR1 - user defined 1
SIGUSR2 - user defined 2
*SIGPOLL - pollable event
*SIGPROF - profiling timer expired
*SIGSYS - bad syscall
*SIGTRAP - trace/breakpoint trap
SIGURG - urgent data available on socket
*SIGVTALRM - signal raised by timer counting virtual time: "virtual timer expired"
*SIGXCPU - CPU time limit exceeded
*SIGXFSZ - file size limit exceeded


Lastly, if you do not wish for your process to send error messages to the terminal, you can launch them into the background, & redirect error messages to /dev/null.

CODE

$  top 2> /dev/null &


In this situation, the 2 represents Standard Error File Descriptor. You have the following 3 File Descriptiors available.

QUOTE

0 : Standard Input (Generally the Keyboard)
1 : Standard Output (Generally the Monitor)
2 : Standard Error


It is important to remember, that just because you are running a process in the background does not make it a daemon process! A background process is one which depends on terminal. Once the terminal is closed, the background process will die. However,if it is daemon process, it executed by init, so it will continue until the operating system is shutdown.


For more information, see the Unix/Linux man pages on fg and bg.
Tom9729
Interesting. I never knew about the "fg" command. icon_up.gif

One thing you didn't mention was the pidof command, which gives you the PID's of all the programs running that match the specified name.

CODE

tom@midnight:~$ pidof xterm
3202 3201


Also, if you don't know the PID and are too lazy to find out, you can simply issue the "killall" command.

CODE

tom@midnight:~$ killall xterm


Also, you can send a process to the background using CTRL + Z (until now, I had no idea how to bring it back biggrin.gif).
no2pencil
QUOTE(Tom9729 @ 4 Jan, 2008 - 12:38 PM) *

Interesting. I never knew about the "fg" command. icon_up.gif

Also, you can send a process to the background using CTRL + Z (until now, I had no idea how to bring it back biggrin.gif).

Using control + Z, if you type exit, you will receive an alert that you cannot leave because you have processes running. Glad you found this informative, & thank you for adding more information!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.