Explore

Loading

Sunday, February 12, 2012

red hat linux automated tasks utilities.


In Linux, tasks can be configured to run automatically within a specified period of time, on a specified date, or when the system load average is below a specified number. Red Hat Linux comes preconfigured to run important system tasks to keep the system updated. For example, the slocate database used by the locate command is updated daily. A system administrator can use automated tasks to perform periodic backups, monitor the system, run custom scripts, and more.
Red Hat Linux comes with four automated tasks utilities: cron, anacron, at, and batch.
Cron
Cron is a daemon that can be used to schedule the execution of recurring tasks according to a combination of the time, day of the month, month, day of the week, and week. Cron assumes that the system is on continuously. If the system is not on when a task is scheduled, it is not executed. To configure tasks based on time periods instead of exact times Anacron.
To use the cron service, you must have the vixie-cron RPM package installed, and the crond service must be running. To determine if the package is installed, use the rpm -q vixie-cron command. To determine if the service is running, use the command /sbin/service crond status.
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

Each line in the /etc/crontab file represents a task and has the format:
minute   hour   day   month   dayofweek   command
·         minute — any integer from 0 to 59
·         hour — any integer from 0 to 23
·         day — any integer from 1 to 31 (must be a valid day if a month is specified)
·         month — any integer from 1 to 12 (or the short name of the month such as jan, feb, and so on)
·         day of week — any integer from 0 to 7, where 0 or 7 represents Sunday (or the short name of the week such as sun, mon, and so on)
·         command — the command to execute (The command can either be a command such as ls /proc >> /tmp/proc or the command to execute a custom script that you wrote.)
For any of the above values, an asterisk (*) can be used to specify all valid values. For example, an asterisk for the month value means execute the command every month within the constraints of the other values.
A hyphen (-) between integers specifies a range of integers. For example, 1-4 means the integers 1, 2, 3, and 4.
Anacron
Anacron is a task scheduler similar to cron except that it does not require the system to run continuously. It can be used to run the daily, weekly, and monthly jobs usually run by cron.
To use the Anacron service, you must have the anacron RPM package installed and the anacron service must be running. To determine if the package is installed, use the rpm -q anacron command. To determine if the service is running, use the command /sbin/service anacron status.
28.2.1. Configuring Anacron Tasks
Anacron tasks are listed in the configuration file /etc/anacrontab. Each line in the configuration file corresponds to a task and has the format:
period   delay   job-identifier  command
·         period — frequency (in days) to execute the command
·         delay — delay time in minutes
·         job-identifier — description of the task, used in Anacron messages and as the name of the job's timestamp file, can contain any non-blank characters (except slashes)
·         command — command to execute
For each task, Anacron determines if the task has been executed within the period specified in the period field of the configuration file. If it has not been executed within the given period, Anacron executes the command specified in the command field after waiting the number of minutes specified in the delay field.
After the task is completed, Anacron records the date in a timestamp file in the /var/spool/anacron directory. Only the date is used (not the time), and the value of the job-identifier is used as the filename for the timestamp file.
Environment variables such as SHELL and PATH can be defined at the top of /etc/anacrontab as with the cron configuration file.
The default configuration file looks similar to the following:
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# These entries are useful for a Red Hat Linux system.
1       5       cron.daily              run-parts /etc/cron.daily
7       10      cron.weekly             run-parts /etc/cron.weekly
30      15      cron.monthly            run-parts /etc/cron.monthly


At and Batch
While cron and anacron are used to schedule recurring tasks, the at command is used to schedule a one-time task at a specific time. The batch command is used to schedule a one-time task to be executed when the systems load average drops below 0.8.
To use at or batch you must have the at RPM package installed, and the atd service must be running. To determine if the package is installed, use the rpm -q at command. To determine if the service is running, use the command /sbin/service atd status.
28.3.1. Configuring At Jobs
To schedule a one-time job at a specific time, type the command at time, where time is the time to execute the command.
The argument time can be one of the following:
·         HH:MM format — For example, 04:00 specifies 4:00AM. If the time is already past, it is executed at the specified time the next day.
·         Midnight — Specifies 12:00AM.
·         Noon — Specifies 12:00PM.
·         Teatime — Specifies 4:00PM.
·         Month-name day year format — For example, January 15 2002 specifies the 15th day of January in the year 2002. The year is optional.
·         MMDDYY, MM/DD/YY, or MM.DD.YY formats — For example, 011502 for the 15th day of January in the year 2002.
·         Now + time — time is in minutes, hours, days, or weeks. For example, now + 5 days specifies that the command should be executed at the same time in five days.
The time must be specified first, followed by the optional date. For more information about the time format, read the /usr/share/doc/at-<version>/timespec text file.
After typing the at command with the time argument, the at> prompt is displayed. Type the command to execute, press [Enter], and type Ctrl-D. More than one command can be specified by typing each command followed by the [Enter] key. After typing all the commands, press [Enter] to go to a blank line and type Ctrl-D. Alternatively, a shell script can be entered at the prompt, pressing [Enter] after each line in the script, and typing Ctrl-D on a blank line to exit. If a script is entered, the shell used is the shell set in the user's SHELL environment, the user's login shell, or /bin/sh (whichever is found first).
If the set of commands or script tries to display information to standard out, the output is emailed to the user.
Use the command atq to view pending jobs. Usage of the at command can be restricted.
Configuring Batch Jobs
To execute a one-time task when the load average is below 0.8, use the batch command.
After typing the batch command, the at> prompt is displayed. Type the command to execute, press [Enter], and type Ctrl-D. More than one command can be specified by typing each command followed by the [Enter] key. After typing all the commands, press [Enter] to go to a blank line and type Ctrl-D. Alternatively, a shell script can be entered at the prompt, pressing [Enter] after each line in the script, and typing Ctrl-D on a blank line to exit. If a script is entered, the shell used is the shell set in the user's SHELL environment, the user's login shell, or /bin/sh (whichever is found first). As soon as the load average is below 0.8, the set of commands or script is executed.
If the set of commands or script tries to display information to standard out, the output is emailed to the user.
Use the command atq to view pending jobs. Usage of the batch command can be restricted.
Viewing Pending Jobs
To view pending at and batch jobs, use the atq command. It displays a list of pending jobs, with each job on a line. Each line is in the format job number, date, hour, job class, and username. Users can only view their own jobs. If the root user executes the atq command, all jobs for all users are displayed.
/       Root file system. Should just contain /bin, /sbin, /dev,
        /root,/lib, and /etc.
/usr    Programmes and source code.
/var    Variable data, such as spools, man pages, news and mail
        queues, database data.
/boot   Boot kernels.
/home   User data and "stuff".
/tmp    Temporary file locations.

Test operators
String test operators - comparisons use
Operator
Meaning
string1 = string2
string1 is equal to string2
string1 != string2
string1 is NOT equal to string2
string1
string1 is NOT NULL or not defined 
-n string1
string1 is NOT NULL and does exist
-z string1
string1 is NULL and does exist
Shell also test for file and directory types
Test
Meaning
-s file
      Non empty file
-f file
      Is File exist or normal file and not a directory
-d dir
      Is Directory exist and not a file
-w file
      Is writeable file
 -r file
      Is read-only file
-x file
      Is file is executable

/dev/null - Use to send unwanted output of program

This is special Linux file which is used to send any unwanted output from program/command.
Syntax:
command > /dev/null
Example:
$ ls > /dev/null

File Manipulation

Ls
list contents of current directory. Examples:
ls file.1 lists presence of file.1
ls -l lists presence of file.1 with expanded information
ls -a lists contents of current directory, including files that start with "."
jot file.1
opens jot editor; reads file.1 into jot for editing
cat file.1
show contents of file.1 on screen
more file.1
show contents of file.1 on screen, but doesn't scroll past top of screen
head file.1
show first 20 lines of file.1 on screen
tail file.1
show last 20 lines of file.1 on screen
diff file.1 file.2
Shows differences between file.1 and file.2
Wc
counts lines, words, and characters in file file.1. Variations:
wc -l counts lines in file file.1
wc -w counts words in file file.1
wc –m counts characters in file file.1
cp file.1 file.2
copies file.1 to file.2. If file.2 exists, it is overwritten. Original file.1 is not removed
mv file.1 file.2
moves file.1 to file.2. If file.2 exists, it is overwritten. Original file.1 is removed
rm file.1
remove file.1. No utilities exist to retrieve it
grep PATTERN file.1
print all lines that contain PATTERN in file.1
lpr file.1
print file.1 on default laser printer
Lpr
Show list of available printers and questions to direct your printing of a file to a particular printer
du –sk
print size (in kilobytes) of current directory ("."). Variations:
du -sk * prints sizes (in kilobytes) of all files and sub-directories
du –sk file.1 prints size (in kilobytes) of file.1
compress file.1
reduce the size of file.1 and replace it with a file named file.1.Z. This compressed file is NOT a (readable) ASCII file.
uncompress file.1.Z
restore the compressed file file.1.Z
find . -name file.1 –print
find the loation of file.1 in the directory and sub-directories. Variations:
find . -name "*pattern" -print finds all files that end with pattern.
find . -name file.1 -exec rm '{}' ';' removes all files named file.1.
grep pattern file.1
print only the lines of file.1 that contain pattern
sort file.1
displays contents of file.1 sorted alphabetically. Variations:
sort -n file.1 displays contents of file.1 sorted numerically
sort -r file.1 displays contents of file.1 sorted alphabetically in reverse order.

Directory Manipulation

Pwd
show the directory that you are in (present working directory)
cd dir.1
change directory to dir.1
mkdir dir.1
make new directory dir.1
rmdir dir.1
remove EMPTY directory dir.1
rm -r dir.1
remove directory dir.2 AND its contents
cp -r dir.1 dir.2
copy dir.1 (and its contents) to dir.2
mv file.1 dir.1
move file.1 to dir.1
Ls
show contents of current directory. Variations:
ls dir.1 shows contents of dir.1
ls -d dir.1 shows PRESENCE of dir.1
du -sk dir.1
show sum of size (in kilobytes) of dir.1 and its contents
tar -cvf dir.1.tar dir.1
store an image of dir.1 and it's contents in file file.1

Process Control

command1&
execute command1 in background
ps –ef
print expanded list of all processes
kill pid1
remove process pid1
<control-c>
interrupt current process
<control-z>
suspend current process
Jobs
display background and suspended processes
kill %1
remove suspended process #1
Top
display the current, most computer-intensive commands
Osview
display the operating system statitistics

No comments:

Post a Comment