LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 02-22-2008, 09:54 AM   #1
anandv_1234
LQ Newbie
 
Registered: Dec 2007
Posts: 8

Rep: Reputation: 0
Question How to run a process in Background?


Dear Folks,

I wanted to run a job in a background, and i wanted to know the difference between running a process in background and foreground.


Thanks and Regards,
V.Anand.
 
Old 02-22-2008, 10:37 AM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
$ background_job &

This will cause the process to detach from your terminal, going into the 'background', allowing you to do other things in the 'foreground'. You can also do this after a job has started with ^Z and then recover it with 'fg'. Check the man page of your shell for full details.
 
Old 02-22-2008, 10:47 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,119

Rep: Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818
In a Bourne-shell compatible shell (Bash, Bourne, Korn):
Code:
progname [arguments] &
When you run something in "foreground" (just type the name and hit return) it runs until it completes and you wait for it. When you run something in background, you immediately get a shell prompt and can do other things while it's running (and a background process runs at a lower priority than a foreground process).

When you expect to have output from the standard output and, maybe the standard error channels:
Code:
progname [arguments] 2>&1 > logfile &
In the above, "[arguments]" means optional command line arguments (you don't use the brackets).

Hope this helps.
 
1 members found this post helpful.
Old 02-22-2008, 12:47 PM   #4
b0uncer
Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Note that a process that requires user input (for example) stops when the input is required, until it's given. In a foreground process you see it's asking for something (or should see if it's sanely programmed), but if it's a background process, it just sits there waiting and won't give a signal unless it's either specifically designed to do so, or you've done something to "watch it". That means the background process that you expect to run in the background for itself, maybe taking a long time, can stop because it wants user input and you wouldn't know it - then when you think it's finished (and are happy because you didn't have to wait it for that long, but were able to do other things) you notice that it hasn't got farther than the beginning because it didn't get it's input So whenever you run processes in the background, make sure they're actually running and not waiting for something like your input..

To find out the state of a process you can use the 'ps' command, for example. The 'jobs' command displays information about jobs you started in that terminal, 'ps' about all processes (depending on the switches used).

In addition to 'fg' there is 'bg'.

Note that both foreground and background processes usually end when their parent process ends, for example when you close a remote shell connection or close the terminal you were using. Usually if you have processes running in the background and type 'exit' to exit, (at least in some shells) you are warned that there are still jobs running (another 'exit' exits nevertheless, but you have the option to end the processes by other means before exiting). To have the processes run even after you close their parent process (log out, for example), you must use a special method of starting the process, for example nohup. With remote connections (when people remotely login, then start a process and want it to keep running after they close the remote connection) a commonly used method is to run the desired program with 'screen' ('screen programname'). Then the program seems to run normally (but it's actually running on screen), and when you want to exit, you can press CTRL+A, then D, to detach ("put the process in the back", make it disappear from the foreground but still have it run there on 'screen'), and just exit - and the process still runs. Later when you come back (maybe re-login remotely) you can run 'screen -d -r' which re-attaches (and possibly de-attaches the process first, if needed) to the process and you get it back to your screen. Typically people run irc clients on remote machines this way, and have the client running even when they're away.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I run updatedb @reboot as a background process? randomsel Slackware 4 02-02-2007 04:36 PM
how to run a process in the X background sh4d0w13 Linux - General 6 09-12-2005 03:54 PM
is there a system call to send process to run in the background? feetyouwell Programming 4 10-01-2004 05:58 AM
how to run any binary in background - background process tej Linux - Newbie 1 08-16-2004 01:27 AM
run process in background using system calls indian Programming 2 08-10-2004 06:03 PM


All times are GMT -5. The time now is 08:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration