Welcome to the new Linux Foundation Forum!

Running processes in the background

Hello.

I know how to run a process in the background:

bigprocess &

Now I get the pid of the process.

After a while I want to view the output of the program.

How can I do that?

Furthermore, how can I send an open process to the background?

Thank you.

Cheers.

Comments

  • bastibbastib Posts: 6
    Hi,

    if I run a job in background here I still see the output in the terminal...
    you can store the output in a file and check that file later like
    ./bigjob.sh > output.txt &
    
    but I guess you know that.

    A running job can be stopped with CTRL+z. If you type
    bg
    
    it continues execution in background. With
    fg
    
    you can reattach it if you want.

    Have you tried screen already?
    With this tool you can start the big job and even close the terminal (or close the connection if it is a remote host) and later reattach it.
    This would work something like that
    screen -d -m bigjob.sh
    
    to reattach:
    screen -d -r
    
    with CTRL+a CTRL+d you can detach again.

    Perhaps it helps...
    Basti
  • linuxtotallinuxtotal Posts: 1
    If you invoke the background process from a window terminal like "xterm" like this

    #> program&

    It's fine, but since the "program" is a child from the terminal process if you accidentely close the terminal you also kill the "program", so you can also use nohup to avoid closing the background process:

    #> nohup program&

    This way the "program" or background process doesnt "hang up" after closing his parent terminal.

    Cheers
  • LegacyUserLegacyUser Posts: 0
    Thank you, guys.

    The screen thing looks very nice. I'll try it later.

    Cheers.
  • disidisi Posts: 11
    Another cool tool is dtach, you can direct the output to a socket and attach to it as you want (runs like a daemon).
    dtach -n /tmp/myprog /usr/bin/myprog

    to attach again:
    dtach -a /tmp/myprog
  • chatpongchatpong Posts: 2
    When I want to run the process in to backgroud, I use the & at the end of command. I bring the background process back with bg and fg command. :) .

    I try to build the article about linux command line in thai language at http://www.LinuxLane.com .

    Cheers
  • altNullaltNull Posts: 65
    alt + F2 will allow you to execute commands from the GUI from a prompt. While the '&' sign will allow you to execute the command in the background, you will also need to turn off any verbose mode options other wise any errors or normal outputs will still print to the command line. Consult the man pages of the command your trying to execute. You can also forward all out put using the '>' symbol after the command followed by a file name.
    Example:
    [email protected]# firefox & > tmp
    


    Hope that helps

    -altNull
  • jabiralijabirali Posts: 157
    altNull wrote:
    [email protected]# firefox & > tmp
    
    Didn't you mean the following?
    firefox > tmp &
    
    That command would write the output from the command "firefox" to a file called "tmp". The command you originally specified would first launch firefox in the background, and then parse "> tmp" as a new command - which would basically pipe standard input to the file, making your terminal into a primitive text editor.

    I want to add that the easiest way to silence an application when you send it to the background, is to simply pipe all its output to /dev/null - which is the digital equivalent of a black hole. Look at the following commands:
    <command> 1>/dev/null &
    <command> 2>/dev/null &
    <command> &>/dev/null &
    
    The first command line would run <command>, redirect all the "normal" output (stdout) to /dev/null and send it to the background. Error messages would still appear in the foreground of your terminal.

    The second line would run <command>, redirect all the errors (stderr) to /dev/null and send the process to the background. Normal messages would still appear in the foreground of your terminal. (I can't really think of many uses for this one for a process in the background.)

    The last command would send both the errors and the normal output to /dev/null, effectively getting rid of all output from the backgrounded process. This is probably the most common way to silence commands.

    Hope this helps :)
  • alphajatinalphajatin Posts: 2
    Whatis the need of running them behind ? Is there any special need of doing that ?
  • jabiralijabirali Posts: 157
    alphajatin wrote:
    Whatis the need of running them behind ?
    It let's you run more applications at a time from your terminal.

    Let's say you're editing a file with the command line editor nano:
    nano /etc/fstab
    
    Then you discover that you want to run some commands in the shell. You can then press Ctrl+Z to hide nano in the background, run whatever commands you want, and then enter the command fg to return to your text editor.

    Another example is if you open graphical applications directly from the terminal. If you try running firefox in the terminal, it will keep you from entering new commands as long as the application is running - in other words, you have to open a new terminal if you want to continue using the command line after launching it. Running firefox & to launch it in the background, prevents this from happening.
Sign In or Register to comment.