Welcome to Dream.In.Code
Getting Help is Easy!

Join 107,308 Programmers for FREE! Ask your question and get quick answers from experts. There are 2,050 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Unix/Linux background processing

 
Reply to this topicStart new topic

> Unix/Linux background processing

no2pencil
Group Icon



post 4 Oct, 2007 - 06:25 AM
Post #1


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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Tom9729
Group Icon



post 4 Jan, 2008 - 12:38 PM
Post #2
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).
Go to the top of the page
+Quote Post

no2pencil
Group Icon



post 4 Jan, 2008 - 12:42 PM
Post #3
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!
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 8/28/08 07:07AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month