School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,087 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,218 people online right now. 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 - 05: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 - 11:38 AM
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 - 11:42 AM
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

web design and seo -er
*



post 2 Aug, 2009 - 12:09 PM
Post #4
How does 'nohup' tie in to this? I ask as I didnt know about bg / fg untill a few seconds ago.
I used nohup recently for a process I want to run permenantly. Is this ok?

Thank you.
Lien
Go to the top of the page
+Quote Post

no2pencil
Group Icon



post 8 Aug, 2009 - 10:49 PM
Post #5
When a NOHUP is issued, it's placing the process into the background, to run as the issuing user, however it's requesting that the OS continues to run the process even after the user disconnects. NOHUP is actually short for "no hangup", & comes from when it was common for users to dial into the server.
Go to the top of the page
+Quote Post

mocker
Group Icon



post 9 Aug, 2009 - 03:19 AM
Post #6
The Linux Screen utility deserves a mention here. As you mentioned, using the & command to send a process to the background does not detach it from the current terminal. Therefore if you are logging in through ssh for example, it will quit running when you exit.

The screen utility essentially launches a new terminal that is detached from your current one. You could log in, start up a screen instance, and start something running, detach from the screen and log out. Later on, you or a coworker could then login and re-attach to the same screen process. Incredibly useful for any admin work

More information at
http://www.rackaid.com/resources/linux-tut...s/using-screen/

It's often not pre-installed so you may need to install it first through your package manager of choice
Go to the top of the page
+Quote Post

syfran
Group Icon



post 31 Aug, 2009 - 01:10 AM
Post #7
QUOTE(Tom9729 @ 4 Jan, 2008 - 11:38 AM) *

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).


Doesn't Control + Z pause the process? Then you can use fg to bring it back again in the foreground or bg to bring it back in the background.
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: 11/21/09 10:52AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month