Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown    



BACK- and FOREGROUND PROCESSES



Like promised in the previous Tip ( "nice" ) now we will tell how to send a process to the background and get it back to the foreground.

There are 2 ways you can send a process to the background, the first is with the "&" sign:

CODE
# nice -n 19 updatedb &

Another way to do this is start it as usual:

CODE
# nice -n 19 updatedb

And then interupt it with Ctrl+Z, and then to continue the process in the background you give the command:

CODE
# bg



Now let us start 2 processes in the background:

CODE
# nice -n 19 updatedb &

This will return something like:

QUOTE
[1] 8697

Meaning job number "1" and process number ( PID ) "8697"
Now the second command:

CODE
# ls -alR / >file-list.txt 2>&1 &

( makes a "file-list.txt" of all the files on your computer and sends the error messages to the same file )

This will return something like:

QUOTE
[2] 8724
[1]  Running                    nice -n 19 updatedb

So the second command gets job number "2" and proces number "8724" and you can see that job number "1" is still running.
Next if we give the command "jobs" we will see:

QUOTE
[2]+  Running                ls -alR / >file-list.txt 2>&1 &
[1]  Done                    nice -n 19 updatedb

You see the first job is "Done" and the second job is still running. Now if we do

CODE
# fg %2

It puts the second job back in the foreground and you will have to wait for the job to finish to get your prompt back. ( or do Ctrl+Z and bg again )

So in a nutshell, the "&" sign at the end of a command puts it in the background, you get a job number and with that job number you can with "fg %<jobnumber>" and "bg %<jobnumber>" put it back and forth.

Now, there are some restrictions:
1). There should no input needed from the keyboad for the background process.
2). Better send output from the process to a file and not to the screen.
3). If you exit the shell the process is stopped ( you can prevent this by preceeding the command with "nohup" )



Bruno


-- Sep 20 2005 ( Revised Dec 10 2005 ) --


Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown