Running Jobs in the Background Lesson 3 / Lesson 5
There are two ways to run a job in the background.
1. Place it in the background as you start the job. 2. Start the job, pause it, and then place it into the background.
For method number one, start the job as normal, but follow the command with an ampersand. So, to start the xeyes program in the background, enter:
xeyes &
The program will now start normally. But, instead of tying up the command-line, you get your command-prompt back so that you can now enter more commands. With xeyes still running, enter:
xclock & jobs
The "jobs" command will show you the list of jobs that are running in the background. Note that one of the job numbers has a "+" sign by it. This means that that job is the default job. Enter:
fg
The default job now comes to the foreground, and you'll lose use of the command-line. To return the job to the background, press the "Ctrl-z" key combination. You'll now see that the job has been stopped. Enter:
jobs
This will show you that one job is running, and the other is stopped. Enter:
bg jobs
This will cause the stopped job to start running in the background, and will show you the status of both running jobs.
Now, let's say that job number two is the default, but you want to bring job number one to the foreground. Enter:
fg 1
Again, you'll lose use of the command-line. Press "Ctrl-z" to pause the job. You should now see that this has become the default job. Return it to the background by entering:
bg
Enter:
jobs
You'll see that both jobs are once again running in the background, and that job number one is still the default.
To terminate these jobs, you can either bring them to the foreground and terminate them as you normally would, or you can find their PID's with "ps" and terminate them with "kill". When you place a job in the background with:
xterm &
You will see output that looks like this: xterm & [4] 1532
The “[4]” is the job number and the “1532” is the process number. You can kill it with either:
kill %4 or kill 1532 Copyright CyberMontana Inc. and BeginLinux.com
All rights reserved. Cannot be reproduced without written permission. Box 1262 Trout Creek, MT 59874
|