Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Linux+ Certification Bible.pdf
Скачиваний:
48
Добавлен:
15.03.2015
Размер:
3.78 Mб
Скачать

388 Part V Maintaining the Linux System

abnormally. If your hard drives were unmounted improperly, they may contain errors and inconsistencies, which fsck will fix before remounting the file system.

This utility can never be run on a mounted file system. You must unmount any file system before checking it with fsck. This is important because fsck won’t work properly if the file system is still in use, and the data it keeps on the drive is still changing.

To check your file system for errors, first unmount the drive:

umount /dev/hdb2

Then, you can run fsck manually on the partition:

fsck /dev/hdb2

The system asks you if you want fsck to fix any errors. Always let fsck fix the errors; otherwise, the file system will likely become more inconsistent through use.

You can use the -a option on fsck to have it automatically fix errors without manual intervention.

Using fsck on a large file system may take quite a long time to complete. If you are running fsck on a large file system manually, try to do it during off-hours so it won’t affect your users.

The fsck utility will run automatically if the system is shutdown abnormally. It will check the file systems for any errors caused by the abrupt shutdown.

System Automation and Scheduling

5.2 Verify user and root cron jobs and understand the function of cron

The daily tasks of running scripts, checking disk space, and performing CPU and memory utilization can take up most of an administrator’s day. By utilizing program scheduling and automation tools, however, these tasks can be performed automatically, and the information and logs sent directly to the administrator’s mailbox.

Two of the most heavily used commands for performing scheduling are at and cron.

at

Use the at command to set a specific command or batch file to run at a certain time. The atd daemon monitors the queue of at commands and executes the commands at their specified time.

Chapter 12 Linux Disk and System Management

389

To use the at command to set a scheduled job, you must first enter the at command with the time you want the job to begin:

at 8:00

This command tells the at command to start the job at 8:00 a.m. The format for the time and date can be fairly complex. If you are just setting the time, you can use the standard 24-hour clock to specify times. For example, 14:00 is 2:00 p.m. You can set a date by using the format dd.mm.yy., or mm/dd/yy. To set a job to start at 11:00 p.m. on February 22, 2002, use the following command:

at 23:00 22.02.01

After you have scheduled the time and date of the job, you can then enter as many commands at the at prompt that you want to schedule for execution. For example, to show the amount of disk space for all local drives to be performed at 11:00 p.m., use the following commands:

at 23:00 at> df -kl <ctrl-d>

Use <ctrl-d> to end the list of commands after you are finished. When the job is run, it automatically mails output results to the user who issued the command. To monitor the current jobs in the queue, use the at -l, or atq command. User at jobs are saved in /var/spool/at/ with a directory for each user that stores all the user’s at jobs.

You can remove jobs from the queue by using the atrm command. First, find the number of the job in the queue by issuing the at -l, then delete it with the atrm command:

atrm 2

The following list includes some of the options that you can use with the at command:

V: Prints the version number to standard error.

q: Uses the specified queue.

m: Sends mail to the user when the job has completed — even if there was no output.

f file: Reads the job from file rather than standard input.

l: Shows completed but not yet deleted jobs in the queue; otherwise, shows the time the job will be executed.

c: Cats the jobs listed on the command line to standard output.

390 Part V Maintaining the Linux System

cron

The disadvantage of the at command is that it can’t schedule recurring jobs. So if you need to run a program every night at 11:00, you have to set it manually by using the at command each time. The cron command, however, can schedule recurring tasks.

The cron command is a powerful scheduling command and should be used by all Linux system administrators for automating tasks and scheduling programs. The system also uses this command to perform system cleanup jobs, such as rotating logs, updating the locate command database, and cleaning up temporary files.

Never delete the system cron files. They automate several important system tasks, including log rotation and temp file cleanup.

The crond daemon controls the cron command and constantly checks the queue for any jobs that need to be run. All cron jobs are stored in the /etc/crontab file. The default crontab file looks similar to the following:

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

# run-parts

 

01

* *

* * root

run-parts /etc/cron.hourly

02

4

*

* * root

run-parts /etc/cron.daily

22

4

*

*

0

root

run-parts /etc/cron.weekly

42

4

1

*

*

root

run-parts /etc/cron.monthly

The first five columns in the preceding example show the specific times that the cron job will run (an asterisk means that any value will work):

minute (0-59)

hour (0-23)

day of month (1-31)

month of year (1-12)

day of week (0-6 — Sunday to Saturday)

In the preceding example, the cron.monthly job will run at 4:42 in the morning on the first day of the month. The cron.hourly job runs at the first minute of every hour, every day. The /etc directory contains several cron subdirectories, which hold scripts that run as stated either hourly, daily, weekly, or monthly.

Know the proper syntax for a cron job command.