Using Advanced Administration Techniques Chapter 11

advertisement
Chapter 11
Using Advanced
Administration
Techniques
Guide to Linux Installation and Administration, 2e
1
Objectives
In this chapter, you will:
• Create shell scripts using basic shell
programming features
• Automate one-time and repetitive tasks using at
and crontab
• Reconfigure the Linux kernel
Guide to Linux Installation and Administration, 2e
2
Valued Gateway
Client:
Writing Shell Scripts
• You can automate the process of entering
frequently used commands by creating a shell
script
• A shell script is an executable file containing
lines of text as you would enter them at a
command line, including special commands to
control the order in which lines in the file are
executed
Guide to Linux Installation and Administration, 2e
3
Valued Gateway
Client:
Interpreting and Compiling Programs
• A compiler converts the source code of a
compiled language into a binary file
• An interpreter converts the source code of an
interpreted language into numeric codes
• A shell script is an interpreted program
Guide to Linux Installation and Administration, 2e
4
Valued Gateway
Client:
Understanding Programming Concepts
• A selection statement lets a computer
programmer determine which parts of a program
will be executed according to values that are
determined as the program is executed
• A loop statement is used to determine whether
part of a computer program should be executed
more than once
• The list of statements controlled by a selection or
loop statement is often called a block, or a
statement block
Guide to Linux Installation and Administration, 2e
5
A Selection Statement
Guide to Linux Installation and Administration, 2e
6
A Loop Repeats a Block of Statements
Guide to Linux Installation and Administration, 2e
7
Valued Gateway
Client:
Components of a Shell Script
• Each script file must start with a line identifying
the interpreter to use for that script
• Each script must have the execute permission set
• Each script must use only correctly formed
statements for the interpreter
Guide to Linux Installation and Administration, 2e
8
Valued Gateway
Client:
Creating a Simple Shell Script
• Some Linux commands are used mostly within
shell scripts
• The read command causes the shell to pause for
a user to enter information at the keyboard
• You can create your own variables to store values
within a script
• Shell scripts use the Linux redirection operators
to change the flow of information to and from
commands
Guide to Linux Installation and Administration, 2e
9
Valued Gateway
Client:
Using Variables in Scripts
• A variable used in a shell script in this way is
sometimes called a shell variable
• To initialize the variable, assign it a value
• A positional variable takes a value based on the
information that the user includes on the
command line
• You indicate positional variables using a dollar
sign and a number
• Positional variables are a useful way to provide
information to the commands in a script
Guide to Linux Installation and Administration, 2e
10
Valued Gateway
Client:
Using if/then/else Tests
• A test is a type of selection statement that lets
you determine if a condition is true or false
• An if command must be followed by a then
command, which lists the commands to be
executed if the test succeeds
• The fi command marks the end of the if statement
• The test command evaluates parameters you
provide and returns either true or false
• The else command adds a block of commands
that are only executed if a test returns a value of
false
Guide to Linux Installation and Administration, 2e
11
Structure of an if/then Statement
Guide to Linux Installation and Administration, 2e
12
File-Testing Operators in the bash
Shell
Guide to Linux Installation and Administration, 2e
13
Structure of an if/then/else Statement
Guide to Linux Installation and Administration, 2e
14
Valued Gateway
Client:
Adding Loops to a Script
• A loop statement causes a block of commands to
be repeated a certain number of times or until a
condition is met
• The for command creates a for loop, which
repeats a statement block once for each list item
• The do and done commands are keywords used
to begin and end a statement block in a for loop
• Loops using for and while repeatedly execute a
statement block based on either a fixed number
of iterations or a condition being tested
Guide to Linux Installation and Administration, 2e
15
Valued Gateway
Client:
Other Scripting Methods
• The statements in a shell script must follow
specific syntax rules
• The specific syntax rules a script must follow
depend on the interpreter that will execute the
script
• Different scripting languages are used for
different purposes
• Scripts often use standardized file extensions to
help users identify them
Guide to Linux Installation and Administration, 2e
16
Popular Scripting Languages
Guide to Linux Installation and Administration, 2e
17
Valued Gateway
Client:
perl Scripts
• Perl
– A programming language developed by Larry Wall
– Especially well suited to processing text strings
– Was very popular as a tool for managing data
submitted by Web page forms
– perl scripts interact with Web servers using a
communication standard called the Common
Gateway Interface, or CGI
Guide to Linux Installation and Administration, 2e
18
Valued Gateway
Client:
Scripts for Graphical Programs
• Tcl/Tk (tool control language/toolkit) is a scripting
language developed by John Ousterhout
• Scripts written in Tcl/Tk are executed by an
interpreter called wish
• The Python scripting language uses the same Tk
graphical programming toolkit as Tcl
Guide to Linux Installation and Administration, 2e
19
A program Written as a Python Script
Guide to Linux Installation and Administration, 2e
20
Valued Gateway
Client:
Automating Tasks with at and crontab
• The at command lets you define one or more
commands to be executed at some future time
• The crontab command lets you define one or
more commands that will be executed repeatedly
at intervals that you designate
• The at command relies on a daemon called atd
• The crontab command relies on a daemon called
crond
• A scheduled task is often called a job
Guide to Linux Installation and Administration, 2e
21
Valued Gateway
Client:
Automating One-Time Tasks
• You can perform a task automatically at some
future time
• You can use the at command to schedule the
task for future execution
Guide to Linux Installation and Administration, 2e
22
Valued Gateway
Client:
Using the at Command
• To automate a task with the at command, you can
either enter commands directly at the command
line, or you can list them in a file
• The atd daemon will check once per minute for
any jobs that have been scheduled using the at
command
• The time specification in the at command is very
flexible
Guide to Linux Installation and Administration, 2e
23
Example Time Specifications
Using the at Command
Guide to Linux Installation and Administration, 2e
24
Valued Gateway
Client:
Using at Interactively
• The only disadvantage of entering commands
interactively is that you cannot alter a command
after it has been edited
• After entering all the commands that you want the
atd daemon to execute, you indicate that you
have finished by pressing Ctrl+D
• You can use the tty command to send output
from a command to the location where you are
currently logged in
Guide to Linux Installation and Administration, 2e
25
Valued Gateway
Client:
Using the batch Command
• The batch command runs your commands when
the system load average drops below 0.8
• The batch command is similar to the at command
and also relies on the atd daemon
• The batch command accepts tasks for future
execution without a fixed time
• Commands are executed when the system is not
busy
Guide to Linux Installation and Administration, 2e
26
Valued Gateway
Client:
Automating Recurring Tasks
• You can use the crontab command to execute
commands automatically at regular intervals
• Many administrators place scripts in the cronrelated directories instead of creating their own
cron job definitions
• In Red Hat Linux 7.3, the /etc directory contains
subdirectories named cron.hourly, cron.daily,
cron.weekly, and cron.monthly
Guide to Linux Installation and Administration, 2e
27
Valued Gateway
Client:
Managing Automated Tasks
• Submitted commands for future execution using
at or crontab can be viewed and deleted
• The root user can view or modify jobs submitted
by any user on the system
Guide to Linux Installation and Administration, 2e
28
Valued Gateway
Client:
Checking the Status of Jobs
• The two daemons atd and crond are started when
you boot Linux using standard service scripts in
/etc/rc.d/init.d
• You can use those scripts to stop and restart the
daemons
• Jobs submitted using at are stored in the
/var/spool/at directory; jobs submitted using
crontab are stored in the /var/spool/cron directory
• The atq and atrm commands are used to manage
at jobs that are awaiting execution
• You should not directly edit a crontab file in the
/var/spool/cron directory
Guide to Linux Installation and Administration, 2e
29
Valued Gateway
Client:
Controlling Access to at and crontab
• You can restrict access to at and crontab so that
only certain users can use these commands
• /etc/cron.allow: Contains usernames that are
allowed to use the crontab command
• /etc/cron.deny: Contains usernames that are not
allowed to use the crontab command
• /etc/at.allow: Contains usernames that are
allowed to use the at command
• /etc/at.deny: Contains usernames that are not
allowed to use the at command
Guide to Linux Installation and Administration, 2e
30
Valued Gateway
Client:
Modifying the Linux Kernel
• You can modify the Linux kernel to activate or
deactivate features, including making features
built-in instead of loadable modules
• You can recompile the Linux kernel from source
code, creating a new kernel
• The file vmlinuz contains the Linux kernel
Guide to Linux Installation and Administration, 2e
31
Valued Gateway
Client:
Installing Kernel Source Code
• To create a new kernel, make certain the kernel
source code and supporting packages are
installed on your system
• The kernel source code is available as an rpm
package or as raw source code from
www.linuxhq.com
Guide to Linux Installation and Administration, 2e
32
Valued Gateway
Client:
Configuring Kernel Features
• Three kernel configuration utilities are supported:
– config,
– menuconfig
– xconfig
• Kernel configuration utilities are started using the
make command
• The make command is a programming utility that
uses instructions in a configuration file (called
Makefile) to execute a series of instructions
Guide to Linux Installation and Administration, 2e
33
The config Utility for Setting kernel
Options
Guide to Linux Installation and Administration, 2e
34
The menuconfig Utility for Setting
kernel Options
Guide to Linux Installation and Administration, 2e
35
The xconfig Utility for Setting kernel
Options
Guide to Linux Installation and Administration, 2e
36
Valued Gateway
Client:
Compiling a New Kernel
• After configuration is completed the make
command is used to start the actual kernel
compilation process
• You also compile and install new modules for the
new kernel as a separate operation
• The make bzImage command creates a
compressed kernel image called bzImage in the
directory /usr/src/linux-2.4/arch/i386/boot
• To use that kernel, you should make a backup
copy of your current kernel in /boot, then copy
the bzImage file to /boot
Guide to Linux Installation and Administration, 2e
37
Summary
• Shell scripts are used to execute a complex set of
commands by entering a single script name
• Each script file starts with a line identifying the
interpreter, has the execute permission set, and
uses only correctly formed statements for the
interpreter
• The if/then/else statement tests a condition and
executes statements if the condition is present
• Loops using for and while repeatedly execute a
statement block based on either a fixed number of
iterations or a condition tested
• Many scripting languages are used on Linux
systems, including perl, PHP, Python,and Tcl/Tk
Guide to Linux Installation and Administration, 2e
38
Summary
• The at command schedules a task for execution
by atd at some future point
• Tasks scheduled using at can be managed using
the atq and atrm commands
• The crontab command schedules tasks that are
to be executed on a regular basis, such as daily
or weekly
• The kernel source code is available as an rpm
package or as raw source code
• Three kernel configuration utilities: config,
menuconfig, and xconfig are started using the
make command
Guide to Linux Installation and Administration, 2e
39
Download