Title: Using Advanced Administration Techniques
1Chapter 11
- Using Advanced Administration Techniques
2Objectives
- 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
3Writing Shell Scripts
Valued Gateway Client
- 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
4Interpreting and Compiling Programs
Valued Gateway Client
- 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
5Understanding Programming Concepts
Valued Gateway Client
- 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
6A Selection Statement
7A Loop Repeats a Block of Statements
8Components of a Shell Script
Valued Gateway Client
- 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
9Creating a Simple Shell Script
Valued Gateway Client
- 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
10Using Variables in Scripts
Valued Gateway Client
- 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
11Using if/then/else Tests
Valued Gateway Client
- 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
12Structure of an if/then Statement
13File-Testing Operators in the bash Shell
14Structure of an if/then/else Statement
15Adding Loops to a Script
Valued Gateway Client
- 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
16Other Scripting Methods
Valued Gateway Client
- 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
17Popular Scripting Languages
18perl Scripts
Valued Gateway Client
- 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
19Scripts for Graphical Programs
Valued Gateway Client
- 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
20A program Written as a Python Script
21Automating Tasks with at and crontab
Valued Gateway Client
- 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
22Automating One-Time Tasks
Valued Gateway Client
- You can perform a task automatically at some
future time - You can use the at command to schedule the task
for future execution
23Using the at Command
Valued Gateway Client
- 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
24Example Time Specifications Using the at Command
25Using at Interactively
Valued Gateway Client
- 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 CtrlD - You can use the tty command to send output from a
command to the location where you are currently
logged in
26Using the batch Command
Valued Gateway Client
- 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
27Automating Recurring Tasks
Valued Gateway Client
- You can use the crontab command to execute
commands automatically at regular intervals - Many administrators place scripts in the
cron-related 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
28Managing Automated Tasks
Valued Gateway Client
- 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
29Checking the Status of Jobs
Valued Gateway Client
- 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
30Controlling Access to at and crontab
Valued Gateway Client
- 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
31Modifying the Linux Kernel
Valued Gateway Client
- 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
32Installing Kernel Source Code
Valued Gateway Client
- 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
33Configuring Kernel Features
Valued Gateway Client
- 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
34The config Utility for Setting kernel Options
35The menuconfig Utility for Setting kernel Options
36The xconfig Utility for Setting kernel Options
37Compiling a New Kernel
Valued Gateway Client
- 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
38Summary
- 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
39Summary
- 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