2004.11_Command Line-Job Control in the Shell.pdf
(
2614 KB
)
Pobierz
Layout 1
LINUX USER
Command Line
Line”, we looked at
screen
, a tool that
gives you multiple virtual consoles in
a single terminal window. If you do not
have
screen
, and the number of consoles
you can launch is limited, it makes sense
to know how you can launch a program
as a background task, interrupt a
program, send a program into the back-
ground, or restore a program to the
foreground. Read on to discover how you
can keep control over your jobs.
Nice Job
The right commands make child’s play of job control in the shell, allowing you
to launch commands in the background, interrupt processes, monitor multiple
jobs, and restore specific jobs to the foreground.
BY HEIKE JURZIK
Into the Background
Let’s imagine a situation where a com-
mand you have launched is taking a
while to complete. You sit there twid-
dling your thumbs while the terminal
waits for the program to complete. And
you cannot enter any other commands
while you are waiting.
It makes sense to move the process to
the background when you are launching
it. To do this, just add an ampersand (&)
to the command, for example:
prompt. To send the task into the back-
ground type
bg
(for “
b
ack
g
round”):
$
[3]- Done
sleep 5
$
bg
[1]+ find . -name bla
> /tmp/list &
Shortcuts
Bash surprises even experienced users
with neat shortcuts and variables that
make the administrator’s life so much
easier.
For example, the exclamation mark
can be used as a variable to represent the
process ID of the last background
process to be launched.
The following commands first display
the process ID in addition to the job ID
(
jobs -l
), and then run the
echo
command
to display the process ID for the last
background process to be launched (
$!
):
This output displays both the job ID and
the complete process name, followed by
the ampersand character. And it shows
you that the process is now running in
the background.
If you have more than one stopped job
running in the shell, the
bg
needs details
on the process you want to run in the
background. By default
bg
moves the
process with the highest job ID to the
background.
You can again use the
jobs
command
to check the job IDs. Then add a per-
cent sign and the job ID to the
bg
command:
$
find . -name bla > /tmp/list &
[1] 664
This command displays a few messages
about the process you launched and then
immediately restores the prompt. The
message includes the job ID in angled
brackets, followed by the PID (
P
rocess
ID
entifier). The PID is unique through-
out the system, whereas the job ID is a
consecutive number assigned by the
shell.
The
jobs
command tells you which
jobs are running in the current shell:
$
sleep 100 &
$
jobs -l
[1]+ 1057 Stopped sleep 1000
[2] 1058 Running sleep 1000 &
[3]- 1066 Running sleep 100 &
$
echo $!
1066
$
jobs
[1]- Stopped find .
-name blub > /tmp/list
[2]+ Stopped find .
-name bla > /tmp/list
$
bg %2
[2]+ find . -name bla
>/tmp/list &
$
jobs
[1]+ Stopped find .
-name bla > /tmp/list
You can put this information to good use
to interrupt the last process. We do not
need the
fg
command (the command
would need the job ID) and [Ctrl-Z] this
time because we will be using the
kill
command instead.
If you forget to add an ampersand when
launching a time-consuming command,
and you have the task running in the
foreground, you may become very impa-
tient and wish to work on something
else. In this situation, the commands to
send this task into the background are
quite simple in the Bash shell.
First, press [Ctrl-Z] to interrupt the
task. The output of the [Ctrl-Z] com-
mand tells you the job ID and the name
of the command you just interrupted
before the shell again displays the
Back to the Foreground
The
fg
(“
f
ore
g
round”) command restores
a background job to the foreground.
Again, the command needs to know
which job you mean, for example:
Command Line
Although GUIs such as KDE or GNOME are
useful for various tasks, if you intend to get
the most out of your Linux machine, you will
need to revert to the good old command
line from time to time. Apart from that, you
will probably be confronted with various
scenarios where some working knowledge
will be extremely useful in finding your way
through the command line jungle.
fg %2
Restoring a job to the foreground means
tying up the shell while the foreground
process completes. A shell prompt tells
you when this is the case:
84
November 2004
www.linux-magazine.com
Job control in the shell
I
n the previous issue of “Command
Command Line
LINUX USER
kill
can do more than just
shoot processes down in
flames; in fact, it can send
them all kinds of signals. For
an overview, type
kill -l
(or
read the manpage
man 7 sig-
nal
). In this case, we will be
using the
-STOP
signal:
The
nice
command assigns
processes a specific priority –
this is useful if you have a
program running in the back-
ground and do not want to
risk losing control over the
system load. Non-privileged
users are only permitted to
assign lower priorities to their
own tasks.
Processes are assigned a
nice
value of 0 by default,
where -20 is the highest, and
19 the lowest priority. The
nice
command is followed by
the priority and the com-
mand, as follows:
Sfmby!boe!fokpz!uif!Xfc!
$
kill -STOP $!
$
jobs -l
[1]- 1057 Stopped
sleep 1000
[2] 1058 Running
sleep 1000 &
[3]+ 1066 Stopped
(signal)
sleep 100 &
Shy Guy
The
nohup
command gives
processes the ability to con-
tinue running when you quit
the shell. To unhitch the
process from the shell, type
nohup
, plus the command
name, and add an ampersand
to send the process to the
background. The following
output
nice -10 find .
U
-name bla > /tmp/list
If you then call the
ps
com-
mand to view the process
status, you will notice that
the
find
call has been
“niced”:
$
ps auxwww
[...]
huhn 1200 0.2 0.6
1520 404 pts/7 RN+
23:02 0:00 find . -
name bla [...]
$
nohup sleep 1000 &
[1] 1116 nohup:
appending output to
`nohup.out'
The
top
command gives you
an even better way of discov-
ering a program’s
nice
level.
The
top
program lists
processes, and sorts them by
CPU usage. The fourth col-
umn, the one with the
NI
heading, tells you the
nice
level (Figure 1).
Pqfsb!cspxtfs!.!b!tbgfs!boe!gbtufs!
Joufsofu!fyqfsjfodf/
tells you that the process will
go on running even if you
type
exit
or press [Ctrl-D] to
quit the shell. Later on, you
can check the
nohup.out
file
to see what the program did
while you were away.
¦!Qpq.vq!ljmmfs
¦!Joufhsbufe!tfbsdi
■
¦!Ubccfe!cspxtjoh
¦!Tqbn.gjmufsfe!f.nbjm
¦!Bewbodfe!tfdvsjuz
¦!Dsptt.qmbugpsn
Hfu!Pqfsb!gps!Mjovy!bu;!
xxx/pqfsb/dpn0mjovynbh
Figure 1: The “top” command also tells you a program’s “nice” level.
www.linux-magazine.com
November 2004
85
Plik z chomika:
Kapy97
Inne pliki z tego folderu:
2010.10_Task Force-Command-Line Task Managers.pdf
(515 KB)
2010.10_Plan on It-Ultimate Gtd System with Org-Mode.pdf
(708 KB)
2010.10_Pdf Power-Creating Pdfs from the Command Line.pdf
(700 KB)
2010.10_Barely There-Lightweight Content Management with Barebones.pdf
(647 KB)
2010.09_One Fell Swoop-Batch Image Conversion with Converseen.pdf
(494 KB)
Inne foldery tego chomika:
Beginners
Business News
Comment
Community
Community Notebook
Zgłoś jeśli
naruszono regulamin