Title: INTRODUCTION TO UNIX
1INTRODUCTION TO UNIX
- SHELL PROGRAMMING, COMPUTATION NUMBER
PROCESSING - SESSION 4
2Multiline commands
- A command line entered at the shell may be longer
than the width of the terminal if you are typing
a complex pipeline expression - UNIX provides a continuation mechanism which can
be used to go on to the next line without
processing the current input - The \ operator escapes the NEWLINE and allows
you to format complex commands for more
readability
3Multiline commands
- You may also type past the end of a line and most
systems will continue your command on the next
line - In some cases, the shell will ignore the NEWLINE
such as when you have begun a quoted command
argument and pressed NEWLINE before closing the
quotes - cat/etc/profile \
4Multiline commands
- To indicate that you are continuing a command
line, the shell will use the PS2 prompt gt
instead of the PS1 prompt - The here Documents argument is an example of a
command line argument which usually flows across
multiple lines
5Multiline commands
- The ltlthereDocument operator provides input data
in a similar fashion to the file redirection
operatorlt except that the data is entered right
in the command string
6Multiline commands
- Using ltlthereDocument
- cat - ltltend
- gt hello this is a test
- gt this is still a test
- gt end
- hello this is a test
- this is still a test
7Multiline commands
- Using ltlthereDocument
- cat - ltltend
- gtMy login id is LOGNAME
- gt end
- My login name is hal
8Multiline commands
- Using ltlthereDocument
- cat - ltltmarker
- gt echo There are ls HOME l wc-l
- gt files in HOME
- gt marker
- There are 23 files in /usr/hal
9Storing shell commands in files
- Commands may be stored in a file and executed in
the same way as they would be if there were
directly entered via the terminal - Command files may be executed by using the file
as an argument for the sh command
10Storing shell commands in files
- cat cmd.file
- echo LOGNAME
- pwd
- sh cmd.file
- hal
- /usr/hal
- Command files may be made directly executable
using the chmod command
11Storing shell commands in files
- chmod u x cmd.file
- cmd.file
- hal
- /usr/hal
- Command files or shell scripts can include all
functions that can be performed directly from the
keyboard
12Storing shell commands in files
- Local variables and path changes may take place
within the shell script which will not affect
your environment when the script finishes - cat cmd3.file
- pwd
- echo Changing directory.....
- cd /usr/hal
13Storing shell commands in files
- pwd
- cmd3.file
- /usr/bob
- Changing directory....
- /usr/hal
- pwd
- /usr/bob
14Storing shell commands in files
- Shell scripts may be used as commands within
other shell scripts but locally defined variables
will not be passed unless the exportcommand is
used - Shell programs saved in files should include
comments to remind the user of what they do and
how they are set up
15Storing shell commands in files
- Comments are prefixed by a operator and
everything after the is ignored by the shell - echo hello this is a comment
- hello
16The if operator
- The if operator provides a conditional test in
a shell script and introduces a multiline command
sequence that closes when the fi operator is
used - Using the if fi operators
- if true
- gt then
17Storing shell commands in files
- gt echo hello
- gt fi
- hello
- if false
- gt then
- gt echo hello
- gt fi
18Storing shell commands in files
- note true returns a value of zero while false
returns a non-zero value - The else operator provides a second leg for
the if construction
19Storing shell commands in files
- if false
- gt then
- gt echo hello
- gt else
- gt echo goodbye
- gt fi
- goodbye
20Storing shell commands in files
- The elif (else if) operator provides additional
nested if-type constructions - if false
- gt then
- gt echo hello
- gt elif true
- gt then
21Storing shell commands in files
- gt echo goodbye
- gt fi
- goodbye
22The testcommand
- The test operator allows a comparison between
two numbers of or returns a true/false value if a
specified file exists/does not exist - The test operator is used in conjunction with
the if construction and may be implemented in
two fashions
23The testcommand
- if test VAR
- gt then
- gt echo defined
- gt fi
-
- if HOME
- gt then
24The testcommand
- gt echo defined
- gt fi
- defined
- The test operator has a number of different
arguments for testing files including
25The testcommand
- -f filename (true exists)
- -r filename (true exists readable)
- -w filename (true exists writeable)
- -x filename (true exists executable)
- -d dirname (true exists is a dir.)
- -s filename (true exists is gt 0 bytes)
26The testcommand
- DIR pwd
- if DIR HOME
- gt then
- echo In home directory!
- gt fi
- In home directory!
- Note the operator is used only between
strings and not for numbers, also the ! (not
equal) operator may be used
27The testcommand
- The test operator has numeric operators for
making comparisons including - -eq (equals)
- -ne (not equals)
- -gt (greater than)
- -lt (less than)
- -le (less than or equal)
28The testcommand
- -o (or)
- -a (and)
- mkdir /tmp/SS
- RET?
- if RET -eq 0
- gt then
- gt echo mkdir succeeded
29The testcommand
- gtelse
- gt echo mkdir failed
- gt fi
- mkdir succeeded
30The exit command
- The exit command is used to immediately end the
execution of a shell script it may also take an
argument that will become the value that the
script returns back to the shell - Using the exit command
- if true
- gt then
- gt exit 6
31The exit command
- gt fi
- gtVAL?
- gt echo VAL
- 6
32The exprcommand
- The expr (expression) command takes numbers and
arithmetic operators as arguments and computes
the result - the standard arithmetic operators are used (, -,
, /, ) and only integers may be processed - expr 4 5
- 9
-
- expr 3 \4 2 V 2
33The exprcommand
- 13
-
- note the and / operators must be preceded
by a \ to keep the shell from interpreting them
incorrectly in this context
34The for operator
- The for operator provides looping capability
within shell scripts so that recursive operations
can be programmed - The for operator can be used in the following
ways - for VAL in 1 2 3 4
- gt do
35The for operator
- gt echo VAL
- gt done
- 1
- 2
- 3
- 4
- for FILE in
36The for operator
- gt do
- gt echo FILE
- gt done
- file1
- textfile
- datafile
37The while operator
- The while operator combines features of both
for and if - If the test is true then the do-done loop is
performed - If the test proves false, the loop is terminated
38The while operator
- while !-f textfile
- gt do
- gt echo Trying to create file
- gt touch textfile
- gt done
-
- VAL1
- while VAL -lt 11
- gt do
39The while operator
- gt touch fileVAL
- gt VALexpr VAL 1
- gt done
-
- note the operator until may be substituted for
while so that the loop is executed as long as
the test is false
40The case operator
- The case operator acts like a series of
if-elif-elif-...elif-fi operators by matching a
character string and executing a list of commands
while unmatched strings are ignored - The case operator is used in the following
fashion
41The case operator
- case LOGNAME in
- gt hal)
- gt echo hello hal, whats up?
- gt
- gt marysue)
- gt echo hi, how are you?
- gt
- gt jim)
- gt echo hello jim, whats doing?
42The case operator
- gt
- gt )
- gt echo hello LOGNAME
- gt
- gt esac
- hello hal, whats up?
43The printf command and output from shell scripts
- The printf command is used to get more complex
forms of character output from a shell script it
provides a string-oriented subset of the
capabilities of the C Languages printf(3)
subroutine - The command takes two kinds of arguments
- A formatting specification
44The printf command and output from shell scripts
- A list of strings to be printed
- printf this is a string s\n 123456
- this is a string 123456
- Strings may be left-justified, truncated, set to
a specific width, and tabs and newlines may be
specified
45The .profile and /etc/profile scripts
- The .profile and /etc/profile shell scripts
are good examples of how command files can be
used to tailor your environment - The /etc/profile script is executed as soon as
a user logs onto the system and provides a set of
default environmental settings for the user - The .profile script resides in the users home
directory and contains settings for that
particular users needs
46The .profile and /etc/profile scripts
- Examples of shell scripts may be found in the
following UNIX system files /usr/bin/calendar,
/usr/bin/spell, /bin/basename, and
/usr/bin/uuto.
47The .profile and /etc/profile scripts
- The . operator provides a means of running a
shell script under the current shell rather than
in its own subshell since changes made in
subshells do not propagate back to the parent
shell - This can be useful if you are testing
modifications to your .profile script
48Command line arguments
- Operators are provided for use in shell scripts
to process command line arguments in the same
fashion as other system provided commands - The argument contains the number of command
line parameters in a shell script - The argument holds the current value of the
parameters
49Command line arguments
- cat - gt echo.args
- echo
- for VAR in
- do
- echo VAR
- done
- chmod ux echo.args
50Command line arguments
- echo.args one two three
- 3
- one
- two
- three
51Command line arguments
- Each command line argument can be used to pass
parameters into the shell script for use the
set operator may also be used within a shell
script to set default values for use
52Errors and error messages in using shell scripts
- Shell scripts can be run with several options to
provide for error tracing and debugging - The-x operator provides a trace function to
allow a shell script writer to follow the
sequence of a command execution - The -v (verbose) option provides more
information about each command as it is executed
in the script
53Understanding UNIX system documentation
- The UNIX Users manual is the official
documentation for the UNIX system and contains,
somewhere inside, all commands, tools, subroutine
libraries, file formats, utilities, etc. - It is sort of similar to the bible in that
everything is there, you just have to learn how
to understand and interpret it
54Understanding UNIX system documentation
- The manual is a reference document and cannot be
used to learn the UNIX system, you must already
understand UNIX to read and interpret the manual
55Layout of the Users manual
- Manual sections
- Commands and application programs
- commands and application programs supplied with
the standard UNIX implementation including most
of the commands that we have discussed in class
56Layout of the Users manual
- System Calls
- primarily used by software developers for use in
C or Fortran programs to use in new programs - Subroutines
- also used mostly by software developers for
inclusion in new programs
57Layout of the Users manual
- File Formats
- this section documents the format of all
important UNIX files such as /etc/passwd and
terminfo - Miscellaneous
- other, harder to classify areas are documented in
this section including troff and terminal
settings
58Layout of the Users manual
- Games
- games and other fun programs are documented in
this section although your particular system may
not have these implanted - Special Files
- this section provides documentation of device
interface files including disks, tapes, and
terminals as well as local area networks
59Layout of the Users manual
- System Maintenance Procedures
- information about booting the system, diagnosing
hardware problems and other system administrator
jobs contained in this section
60Referring to the manual section of a command
- Manual pages are identified by a topic or command
name followed by a manual section reference in
parenthesis and all commands and topics are
arranged alphabetically in their section - Examples of headings include
- pwd(1), init(1M), man(1), term(5)
61A typical Man page
- Man page sections include
- Heading
- command name (both left and right side), release
information (center) - Name
- name of the command in full form
- Synopsis
- short summary of how to use the command with
syntax options
62A typical Man page
- Description
- more detailed description of the command and all
its options - Note
- additional comments not suited for the
description section - Warnings
- discusses potential problems which can arise when
using the command
63A typical Man page
- See also
- gives related commands or other relevant
information pertaining to the use of the command - Files
- gives the path names of databases and files which
are affected by or which are used by this command - Diagnostics
- gives a short description of error messages which
may be generated by this command
64A typical Man page
- Bugs
- provides warnings about unexpected and
undesirable problems with this command
65The permuted index
- The Permuted Index lists each word of the name
section of each manual page as an aid to locating
the correct section of the UNIX manual - The alphabetic listing appears in the middle of
the page - The manual page listing is on the right side of
the page
66The permuted index
- The words which precede the entry are on the side
of the page
67The online mansystem
- The command man is available on larger systems
to provide access to the actual manual pages from
your terminal - Care must be taken that the proper TERM setting
is specified so that the output to your terminal
is correct
68The online mansystem
- Manual pages are often stored in their troff
source format and are normally processed by
troff -man or nroff -man before being
displayed on your terminal
69Online help command
- UNIX SVR3 provides an online help facility with
its own menus and submenus - To enter this facility from its own menu, enter
the command help and follow the instructions
provided
70Online help command
- help may also be entered by using its subsystem
commands directly from a command line - locate
- usage
- starter
- glossary
71NUMBER PROCESSING UNDER UNIX
- Computation tools under UNIX are primarily line
oriented and are more like programming languages - Very few on-screen calculators, like those found
in MS-DOS productivity tools, are available under
UNIX except for the X-Windows environment
72NUMBER PROCESSING UNDER UNIX
- The tools that are available work at variety of
levels, from the simple 'expr' operator to the C
programming language
73ELECTRONIC SPREADSHEETS
- The standard SVR4 release of UNIX does not
include an electronic spreadsheet - Lotus 1-2-3 and many other spreadsheets have been
ported to UNIX - Spreadsheet software may be purchased from third
party vendors
74SHELL REPRISE
- Fairly complex computational tasks can be
accomplished using shell programming and the
pipes, filters and 'expr' operator - A few special purpose statistical and numerical
computing programs have been designed but most
have not been successful enough to include in the
standard releases of UNIX
75SHELL REPRISE
- The 'stat' package was designed for use with the
shell pipeline but it is not normally included in
most UNIX releases
76THE 'DC' AND 'BC' CALCULATORS
- The 'dc' and 'bc' calculators are powerful
line-oriented numerical processing tools - The 'dc' (desk calculator) operator uses "reverse
polish" notation (postfix notation) which
requires the user to enter the numbers to be
processed, followed by the process operator - The 'dc' (desk calculator) is
- Stack-oriented
- Any number base
- Any precision level
77THE 'DC' AND 'BC' CALCULATORS
- The 'bc' (before calculator) operator uses the
more standard "infix notation" similar to what
you learned in grade school where you enter a
number, followed by a numeric operator, then the
second number - Procedure oriented
- Local subroutines and variables
- Logical operators and statements
- Any precision level
- Often used as a preprocessor to 'dc'
78THE 'DC' COMMAND
- The 'dc' operator reads its commands and values
from standard input, from a file, or both - When 'dc' reads its commands from a file, it
continues until all input has been processed
(EOF) and then returns to standard input for more
commands
79THE 'DC' COMMAND
- When 'dc' reads its commands from standard input,
it continues until a CTRL-D is entered which ends
its execution - When 'dc' receives a 'q' (quit) command from
either the keyboard or a file, it terminates
80THE 'DC' COMMAND
- Instructions to 'dc' may be numbers or operators
- Numbers may contain decimal points and negative
numbers are preceded by the '_' (underscore)
operator numbers are immediately pushed onto the
'dc' stack
81THE 'DC' COMMAND
- dc 3 4pq
- 12
-
- dc
- 3
- 4
-
- p
- 12
- q
82THE 'DC' COMMAND
- The arithmetic operators '' (addition), '-'
(subtraction), '/' (division), ''
(multiplication), '' (remainder), and ''
(exponentiation) act on the first two numbers in
the stack - The result of an operation is placed back on the
stack, replacing the two numbers - The 'p' (print) operator displays the top value
on the stack but does not change it
83THE 'DC' COMMAND
- 'dc' can accept commands and values on separate
lines or multiple commands and values on single
lines delimited by white space these modes may
be intermixed - dc
- 5 6
- 5
- / p
- 6
- q
84THE 'DC' COMMAND
- Several operations in a row may be performed
since the result of the last operation is placed
back on the stack and the next operation will use
that value and the one below it - dc
- 3
- 4
- 7
-
- -
- p
- -8
85THE 'DC' COMMAND
- Computations performed by 'dc' retain as many
decimal places as needed - dc
- 2.345 5.4567 p
- 7.8017
- q
86THE 'DC' COMMAND
- Other commands in 'dc' include
- 'c' (clear stack)
- 'd' (duplicate top element of stack)
- 'f' (print all elements of stack in order)
87THE 'DC' COMMAND
- Variables in 'dc
- The 'dc' command provides 26 register variables
(a-z) to hold values needed for calculations - Register names are lower case alphabetic
characters and can be used in the following way - 's' (save), top of stack in register "a"
- 'l' (get), register "a" to top of stack
88THE 'DC' COMMAND
- Register elements may be reversed by or moved out
of the way by placing them in unused registers
89THE 'DC' COMMAND
- dc
- 2.34
- 4.56
- p
- 4.56
- st
- p
- 2.34
- lt
- p
- 4.56
- q
90THE 'DC' COMMAND
- The 's' and 'l' operators may also be used with
"auxiliary stacks" by using upper case alphabetic
characters (A-Z) as names these stacks preserve
the order of the values just as the main stack
does - Character strings may also be stored on the stack
by using the ' ' brackets and stored strings
can be taken from the stack and executed using
the 'x' operator
91THE 'DC' COMMAND
- Character strings must be explicitly removed
after use or unexpected results may occur - Other commands for 'dc' include
- '!' (bang), the shell escape operator
- 'v' (square root)
- 'i' (number base), for input
- 'o' (number base), for output
92THE 'BC' COMMAND
- The 'bc' calculator is a simplified numerical
calculation programming language processor which
acts as a front-end to 'dc - The 'bc' calculator reads from standard input and
writes to standard output so that redirection
from a file is allowed - Unlike 'dc', when 'bc' reads its input from a
file, it exits when it reads the (EOF) marker
93THE 'BC' COMMAND
- The 'bc' calculator may also take a filename as
an argument in which case it processes the
commands in the file and then returns to standard
input (keyboard) for additional commands - The 'bc' calculator uses "infix" or the standard
notation that most people learn in grade school
(a b c)
94THE 'BC' COMMAND
- The 'bc' calculator provides the user with the
use of variables (a-z) and the concept of number
arrays - Variables are assigned using any lower case
alphabetic character and may be referenced at any
time just like numbers and they retain their
value until reassigned
95THE 'BC' COMMAND
96THE 'BC' COMMAND
- Arrays of numbers may be defined by using the '
' brackets to indicate the array index - s23.3
- The 'bc' command performs its calculations in
integer mode unless the 'scale' operator is used
to indicate the number of decimal places required
97THE 'BC' COMMAND
- bc6.456/5.6781scale36.456/5.5671.137
98THE 'BC' COMMAND
- Different base numbers may be used by setting the
input base 'ibase', and the output base 'obase'
bcibase210019obase8100111
99THE 'BC' COMMAND
- The '' and the '--' operators are used to
increment and decrement the value of a variable
bcs4s4--ss3
100THE 'BC' COMMAND
- bcs4t--s3.3t33.3
- bc
- s4
- t --s 3.3
- t4
- 3.3
101THE 'BC' COMMAND
- Other 'bc' statements and operators are provided
including additional numeric operators, logical
operators and the 'if', 'for, 'while' and 'break'
statements - The following operators are provided to change
the value of a variable directly
102THE 'BC' COMMAND
- '-' (subtract from)'' (add to)'' (multiply
by)'/' (divide by)'' (divide remainder)''
(exponentiate by)
103THE 'BC' COMMAND
- Sequences of operations may be grouped together
by using ' ' brackets which return whatever
values that each individual component would
return - Logical operators may be used with the loop and
test operators ('if', 'for', 'while')
104THE 'BC' COMMAND
- '' (is equal)'lt' (is less than or equal)'gt'
(is more than or equal)'!' (is not equal)'gt'
(is greater than)'lt' (is less than)
105THE 'BC' COMMAND
106THE 'BC' COMMAND
- bcs4while ( s gt 0 ) ss - 14321
107THE 'BC' COMMAND
- bcfor ( s 4 s gt 0 --s ) s4321
108THE 'BC' COMMAND
- The 'break' statement allows exit from a loop
before the condition part tests false,
terminating only the innermost loop - bcfor ( s 4 s gt 0 --s ) if ( s lt 2 )
breaks43
109THE 'BC' COMMAND
- The 'bc' processor allows the creation of
functions identified by a single alphabetic
character (a-z) - External variables may be used within functions
and their values may be changed as a result of
the function
110THE 'BC' COMMAND
- Local variables may be declared using the auto
operator and used within the function without
changing the value of existing external variables
with the same name
111THE 'BC' COMMAND
- bc
- define x (a, b)
- auto s
- for (s a sltb s)
- s
- return (22)
-
- x (3,6)
- 3
- 4
- 5
- 22
112THE 'BC' COMMAND
- The 'bc' processor also provides a math library
which can be loaded with the '-l' command line
option - 's(x)' (sine of x)'c(x)' (cosine of
x)'e(x)' (exponential of x)'l(x)' (log of
x)'a(x)' (arctangent of x)'j(n,x)' (Bessel
function of x)
113THE 'AWK' COMMAND
- The 'awk' command (named for Aho, Weinberger,
Kernighan) is a powerful tool for both
computation and pattern processing tasks which
scans a list of input files for lines that match
a set of specified patterns and then performs a
set of specified actions on those lines
114THE 'AWK' COMMAND
- the 'awk' command uses elements of 'bc', and the
C programming language with features of shell
programming - Interpreted like 'bc'
- Field variables (1, 2 etc.) like shell
- Printing and control operators like C
115THE 'AWK' COMMAND
- The 'awk' command is extremely sophisticated in
its uses while being terse and arcane in its
documentation
116THE UNIX PROCESS
- INTRODUCING THE UNIX PROCESS
117INTRODUCING THE UNIX PROCESS
- A process or task is an executing program some
processes continue as long as you are logged on
to the system while others exist for the duration
necessary to complete their task
118INTRODUCING THE UNIX PROCESS
- Some commands are internal to the shell ('cd' and
'echo' for example) and do not generate a
separate process when executed - Command strings with pipeline operators may
generate more than one process when they are run
119TIME-SHARING IN THE UNIX SYSTEM
- Under the control of the kernel, the multiple
tasks are switched several times a second to give
the user the impression that they are being run
simultaneously - Under heavy loads or when processes are creating
a problem response time will slow down and users
may become aware that they are sharing the system
120CONTROLLING PROCESS PRIORITY
- While most processes have equal priority, that is
they are given equal time by the kernel, certain
processes may need to be given more time than
others to be able to operate effectively - Only the superuser may raise the priority of a
process any user may lower the priority of their
own processes (default 10) using the 'nice'
command
121CONTROLLING PROCESS PRIORITY
- nice cat /etc/passwd
- nice -14 pr printfile
- nice --10 cat bigdocfile
122BACKGROUND PROCESSES
- Most often, the 'nice' command is used in
conjunction with a process running in background
mode to allow the task to be executed while the
user continues to work on the shell - The user may create as many background processes
as they wish
123BACKGROUND PROCESSES
- Processes may be monitored by using the 'ps'
command - nice cat /etc/passwd gt scratch1
- 1234
-
- nice ls -al gt files1 2gt error1
- 1235
-
124LOGGING OFF ...
- Normally, processes that are being run in the
background will be killed when you log off the
system since they are associated with your login
shell - The 'nohup' (no hangup) command will allow a
process to continue to run after the user has
logged off
125LOGGING OFF ...
- When using 'nohup' with a pipeline command, all
elements of the pipeline must be preceded by
'nohup' - If no output file is specified (via redirection)
then 'nohup' will create its own output file
(nohup.out) for the user
126LOGGING OFF ...
- nohup cat /etc/passwd
- 1234
- sending output to nohup.out
-
- nohup cat file1 nohup wc gt out1
- 1235
-
127PARENTS AND CHILDREN
- Processes are said to be 'born' when they begin
and 'die' when they end - When one process spawns another process, the
older process is known as the 'parent' while the
newly created process is called the 'child'
process
128PARENTS AND CHILDREN
- Normally, when a parent process is killed, its
associated child processes are also terminated - The 'nohup' command allows an existing child
process to be 'inherited' by 'init', process
number 1 - A parent process may have multiple children but
child processes may have only one parent process
129THE 'PS' COMMAND
- The 'ps' (process status) allows a user to
display information about processes that are
currently alive - The 'ps' command with no arguments will display
processes associated with your login session - ps
130THE 'PS' COMMAND
- The '-f' option provides more information about
each process for the user including the UID (user
id), PID (process id), PPID (parent process id),
C (resource use index), TTY (terminal executing),
STIME (time started), and COMMAND (process
executed) - ps -f
131ACTIVITY OF OTHER USERS
- The 'ps' command may also be used to give you
information concerning the processes of other
users by using the '-u' option followed by a
valid user id
132ACTIVITY OF OTHER USERS
- The '-a' operator will show all user started
processes currently operating a the system - ps -u hal
- ps -af
- ps -uf hal
133SYSTEM PROCESSES
- The 'ps' option '-e' will give information about
all processes operating on a system including
system processes
134SYSTEM PROCESSES
- This information can be useful in diagnosing
problems with a system once you are familiar with
the processes which should be present and how
much activity they should be receiving from the
cpu - ps -ef
135DIAGNOSING PROBLEMS WITH PROCESSES
- Using the 'ps -ef' command and options, problem
with a system may be recognized and corrected by
the system administrator - When the system response time begins to slow down
or when processes seem to have an extremely large
number of children, this may indicate a problem
an application may show a number of indications
of failure
136DIAGNOSING PROBLEMS WITH PROCESSES
- Its process will die prematurely and/or keep
attempting to respawn (restart) - The process will spawn too many children and
begin to slow up system response - A process may consume inordinate amounts of cpu
time due to a program error (loop) or an improper
priority setting
137KILLING A PROCESS
- A process may be terminated by using the 'kill'
command with the PID of the offending process as
its argument - A user may kill any process they own while the
superuser may kill any process except processes
with PID's 1, 2, 3, 4
138KILLING A PROCESS
- The 'ps' command should be run again to see that
the process was killed and that its children were
not inherited by another parent (grandparent)
process - Multiple processes may be killed at the same time
- kill 1234 1235 1236
139SIGNALS
- When a process is killed, it is sent a
termination "signal" which it may ignore if there
are already problems with that process - The normal 'kill' signal is '15' which tell the
process to terminate
140SIGNALS
- An "unconditional" kill signal ('9') can be sent
to processes that ignore the '15' sent by the
standard kill command - kill -9 1234 1235 1236
141PROCESSES THAT RESPAWN
- The '/etc/inittab' file
- Some processes will automatically restart when
you kill them these processes are listed int the
'inittab' file - Field three of each entry in the file indicates
if the process listed it to restart when
terminated - To terminate a runaway process, you must edit the
'inittab' file, changing the 'respawn' entry to
'off'
142PROCESSES THAT RESPAWN
- After editing the file, use the 'telinit q'
command to instruct 'init' to reread the table
and stop respawning the offending process - Why the first 'ps' takes longer
- Under SVR4, 'ps' maintains a table in the
'/etc/ps_data' file to enable it to speed up the
response time for 'ps'
143PROCESSES THAT RESPAWN
- If the command has not been run for some time or
if the system has been rebooted, 'ps' may
automatically rebuild the table and take a bit
longer to respond
144PROCESSES THAT RESPAWN
- Waiting and defunct processes
- When a process spawns a child, it will normally
wait for the child to complete and go out of
existence - If the process is run in the background, however,
the child may finish and the parent may not
acknowledge its completion, leaving it marked as
a ltdefunctgt process when viewed by 'ps'
145PROCESSES THAT RESPAWN
- A certain number of these ltdefunctgt processes
represents no problem an excessive number
indicates a problem
146PROCESSES THAT RESPAWN
- The '/proc' directory
- A new feature in SVR4, the '/proc' directory
contains virtual memory images' of the processes
that are running on the machine - The file size listed by the 'ls -l' command
indicate the actual memory size used by the
process
147PROCESSES THAT RESPAWN
- '/proc' currently provides little value for UNIX
users but it will provide the basis for future
developments in memory-mapped file systems