Page 1 of 1

Unix/Linux background processing Rate Topic: -----

#1 no2pencil  Icon User is online

  • Dic Head
  • member icon

Reputation: 5175
  • View blog
  • Posts: 26,868
  • Joined: 10-May 07

Post icon  Posted 04 October 2007 - 06:25 AM

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.

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

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

$  fg 26452



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

$  kill -9 26452



Or, you can force it to restart.

$  kill -HUP 26452



Here is a list of the available signals, from wiki http://en.wikipedia....%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.

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

Is This A Good Question/Topic? 0
  • +

Replies To: Unix/Linux background processing

#2 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Posted 04 January 2008 - 12:38 PM

Interesting. I never knew about the "fg" command. :^:

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.

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.

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 :D).
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is online

  • Dic Head
  • member icon

Reputation: 5175
  • View blog
  • Posts: 26,868
  • Joined: 10-May 07

Posted 04 January 2008 - 12:42 PM

View PostTom9729, on 4 Jan, 2008 - 12:38 PM, said:

Interesting. I never knew about the "fg" command. :^:

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

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!
Was This Post Helpful? 0
  • +
  • -

#4 web design and seo -er  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-August 09

Posted 02 August 2009 - 01:09 PM

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
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is online

  • Dic Head
  • member icon

Reputation: 5175
  • View blog
  • Posts: 26,868
  • Joined: 10-May 07

Posted 08 August 2009 - 11:49 PM

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.
Was This Post Helpful? 0
  • +
  • -

#6 mocker  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Posted 09 August 2009 - 04:19 AM

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.c...s/using-screen/

It's often not pre-installed so you may need to install it first through your package manager of choice
Was This Post Helpful? 0
  • +
  • -

#7 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Posted 31 August 2009 - 02:10 AM

View PostTom9729, on 4 Jan, 2008 - 11:38 AM, said:

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


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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1