Chapters 3,8 - gozips.uakron.edu

advertisement
2440:145 Operating Systems
Study Guide #2
The University of Akron
Summit College
Dept of Business Technology
Computer Information Systems
Common UNIX/Linux Supported Editors
 Some of the UNIX supported editors include:
o ed – the original line editor.
o ex – a more sophisticated superset of the ed editor.
o vi – a visual screen editor
o emacs – a powerful public domain screen editor which provides macros, programming tools,
customization and several keyboard shortcuts
o pico or nano – a simple editor which includes onscreen information the Control key sequences
o joe – another simple editor with includes onscreen command information
The vi Editor
 The two versions of the vi editor are the:
o view Editor – has read-only flag set in it. Useful for files not meant to be modified.
o vedit Editor – has several flags set in it. Intended for beginners to make it easier to use vi.
 The two basic modes of operation in vi are:
o Command (edit) mode – the default mode when you enter the vi editor. Key entries are
interpreted as commands. Certain commands start with the colon (:), forward slash (/), or question
mark (?).
o Text Input mode – the keyboard becomes your typewriter. Input is not interpreted as a
command.
Basic vi Commands
 vi – access to the vi editor
 :q – cancels an editing session
 :q! – cancels an editing session and exits the vi editor without saving
 :w – saves a file and continues working
 :wq – saves changes to a disk and exits the vi editor. Can also use ZZ
 :r – reads text from one file, and adds it to another
 :! – runs shell commands while in the vi editor. E.g., :! date (shows the date/time), :!r ! cal 2010 (reads
the calendar of 2010 into the current file
vi Cursor Movements
 h – moves the cursor position one space to the left. Can also be accomplished with [Left Arrow]
 j – moves the cursor position one line down. Can also be accomplished with [Down Arrow]
 k – moves the cursor position one line up. Can also be accomplished with [Up Arrow]
 l – moves the cursor position one space to the right. Can also be accomplished with [Right Arrow]
 0 (zero) – moves the cursor position to the beginning of the current line
 $ – moves the cursor position to the end of the current line
 w – moves the cursor position forward one word (to the beginning of the word)
 e – moves the cursor position forward one word or to the end of the current word
 b – moves the cursor position backward one word or to the beginning of the current word

[Ctrl+d] or [Ctrl+f] – moves the cursor down toward the end of the file 12 or 24 lines respectively
 [Ctrl+u] or [Ctrl+b] – moves the cursor up toward the beginning of the file 12 or 24 lines respectively
 G – moves the cursor to a specified line number or end of a file. E.g. 50G (moves cursor to line 50)
 [Ctrl+g] – checks the line number of the current line
vi Text Input Mode
 i – inserts text before the character the cursor is on
 I – inserts text at the beginning of the current line
 a – appends text after the character the cursor is on
 A – appends text after the last character of the current line
 o – opens a blank line below the current line and places the cursor at the beginning of the new line
 O – opens a blank line above the current line and places the cursor at the beginning of the new line
Document1
Enoch E. Damson
Page 1 of 6
2440:145 Operating Systems
Study Guide #2
The University of Akron
Summit College
Dept of Business Technology
Computer Information Systems
vi Text Correction (Editing)
 x – deletes a specified character
 d – cuts or deletes a specified character to a buffer
 dd – deletes the current line to a buffer
 y – copies or yanks a specified character to a buffer
 yy – copies or yanks the current line to a buffer
 p – pastes or puts any text from a buffer by placing the contents below the cursor
 P – pastes or puts any text from a buffer by placing the contents above the cursor
 u – undo the most recent change
 U – undo all changes on the current line
 r – replaces a character that the cursor is on
 R – replaces characters starting from the cursor position
 . (dot) – repeats the last text changes
 “ – the put operator is used to access the buffer. E.g. “2yy (copy current line to numeric buffer 2), “b3dd
(delete/cut next 3 lines to alphabetic buffer b), “8p (pastes text from the numeric buffer 8).
The vi Buffers
 The two main categories of vi buffers are:
o Numbered buffers – Nine temporary buffers from 1 to 9
o Alphabetic (Named) buffers – 26 buffers from a through z
vi Text Searches
 :/ – searches forward for a specified pattern. E.g., :/Linux (searches for the word “Linux”)
 :? – searches backward for a specified pattern. E.g., :?Linux (searches for the word “Linux”)
UNIX/Linux Shell Variables
 Variables – named buffers to store specific values to control or customize shell environment. The two types of
variables supported by Linux shells are:
o Environment variables – used to keep track of essentials system needs usually in .profile file
o Local variables – user-defined for other tasks by the user
Common UNIX/Linux Shell Environment Commands
 alias – creates an alias for a command. E.g., alias june=”cal 6 2010”
 unalias – removes an alias for a command.
 history – lists all the commands contained in the bash history file
 echo – displays the specified arguments on the output device
 set – establishes specific operational conditions in the bash shell or displays the environment variable settings
 unset – removes an unwanted variable
 grep – selects lines or rows that match a specified patterns of information within a file or command output.
E.g., grep John names.list
 ps – shows processes on a system. E.g., ps aux
 kill – ends a process using the process ID (PID)
 cmp – determines whether two files are identical
 diff or sdiff – compares and selects differences in two files or directories
 sort – sorts and merges multiple files in alphabetical order
 uniq – removes duplicate lines from a sorted text file. E.g., sort data.list | uniq
 sleep – suspends execution for a specified time (in seconds by default). E.g., sleep 60 ; date
 tee – send a program output to the screen and in a file at the same time. E.g., ls | tee files.list
 export – makes a variable an environment variable and exports a specified list of variables to other shells. E.g.,
export VAR1
 fc – list, edit, and re-execute commands in the history list. E.g., fc -l
 nohup – prevents the termination of of the background process when logged out
Document1
Enoch E. Damson
Page 2 of 6
2440:145 Operating Systems
Study Guide #2
The University of Akron
Summit College
Dept of Business Technology
Computer Information Systems
Linux Local Network Communication Commands
 write – used for terminal-to-terminal communication among logged on users
 talk – used for terminal-to-terminal communication among logged on users
 mesg – used prohibit unwanted write messages (n) or receive them (y). E.g. mesg n
 wall – used mostly by Linux system administrators to send messages to the system users
 news – used to lookup latest news in the system. Used by systems administrators to provide latest news
 mail/mailx – provides electronic mail system for Linux users
Other Linux Environment Commands
 du – summarizes the total disk space used by directories/subdirectories on a file system
 df – reports the total amount of disk space free on a file system
 finger – displays detailed information on logged on users
 time – provides information about a command’s computer time (real, user, and system times). E.g. time ls
 calendar – a reminder service that reads a schedule from a calendar file users creates in the current directory
 type – provides more information about another command
 banner – displays a string(usually 10 characters or less) in large letters
 spell – checks the spelling of a specified document or words entered from the keyboard
Linux Archiving and Compression Commands
 tar – copies a set of files into a single file using the options -c (create), -f (file), -v (verbose), -x (extract). E.g.
tar –cvf archivedfile.tar file1 file2 file3 (to archive 3 files), tar –xvf archived.tar (to extract the archived files)
 gzip/gunzip – compresses or decompresses files by reducing the file size
 bzip2/bunzip2 – compresses or decompresses files by reducing the file size
 compress/uncompress – compresses or decompresses files by reducing the file size
Linux Task Scheduling Commands
 at – schedules command execution at a later date only once. E.g. at 13:30 Fri sort mylist
 crontab – uses the cron daemon to execute scheduled periodic commands.
Linux Security Commands
 passwd – changes user password
 chmod – changes the file/directory permissions
 crypt – encrypts and decrypts files usually with a key. E.g. crypt xy < confidential > confidential.cript
Linux Remote Computing Commands
 ftp – file transfer program that transfers files from one system to another using commands such as open,cd, dir,
ls, put/mput, get/mget, close, quit/ bye
 telnet – remote login program that allows remote login to a server for a server’s resources
 rcp – remote copy program
 rsh – remote shell program
 rlogin – remote login program
Cron
Cron is a daemon that executes scheduled commands. Cron is started automatically from /etc/init.d on entering
multi-user runlevels. Cron searches its spool area (/var/spool/cron/crontabs) for crontab files (which are named after
accounts in /etc/passwd); crontabs found are loaded into memory. Note that crontabs in this directory should not be
accessed directly - the crontab command should be used to access and update them.
Cron also reads /etc/crontab, which is in a slightly different format. Additionally, cron reads the files in /etc/cron.d.
Cron then wakes up every minute, examining all stored crontabs, checking each command to see if it should be run
in the current minute. When executing commands, any output is mailed to the owner of the crontab (or to the user
named in the MAILTO environment variable in the crontab, if such exists). The children copies of cron running
these processes have their name coerced to uppercase, as will be seen in the syslog and ps output.
Document1
Enoch E. Damson
Page 3 of 6
2440:145 Operating Systems
Study Guide #2
The University of Akron
Summit College
Dept of Business Technology
Computer Information Systems
Additionally, cron checks each minute to see if its spool directory's modtime (or the modtime on /etc/crontab) has
changed, and if it has, cron will then examine the modtime on all crontabs and reload those which have changed.
Thus cron need not be restarted whenever a crontab file is modified. Note that the crontab(1) command updates the
modtime of the spool directory whenever it changes a crontab.
Special considerations exist when the clock is changed by less than 3 hours, for example at the beginning and end of
daylight savings time. If the time has moved forwards, those jobs which would have run in the time that was skipped
will be run soon after the change. Conversely, if the time has moved backwards by less than 3 hours, those jobs that
fall into the repeated time will not be re-run.
Only jobs that run at a particular time (not specified as @hourly, nor with '*' in the hour or minute specifier) are
affected. Jobs which are specified with wild cards are run based on the new time immediately.
Clock changes of more than 3 hours are considered to be corrections to the clock, and the new time is used
immediately.
In Debian and Redhat cron treats the files in /etc/cron.d as extensions to the /etc/crontab file (they follow the special
format of that file, i.e. they include the user field). The intended purpose of this feature is to allow packages that
require finer control of their scheduling than the /etc/cron.{daily,weekly,monthly} directories allow to add a crontab
file to /etc/cron.d. Such files should be named after the package that supplies them. Files must conform to the same
naming convention as used by run-parts: they must consist solely of upper- and lower-case letters, digits,
underscores, and hyphens. Like /etc/crontab, the files in the /etc/cron.d directory are monitored for changes.
You should use absolute path names for commands like /bin/ls. This is to insure you call the correct command.
Crontab
Crontab is the program used to install, deinstall or list the tables used to drive the cron daemon in Vixie Cron. Each
user can have their own crontab, and though these are files in /var/spool/cron/crontabs, they are not intended to be
edited directly.
Each user has their own crontab, and commands in any given crontab will be executed as the user who owns the
crontab. Uucp and News will usually have their own crontabs, eliminating the need for explicitly running su as part
of a cron command.
Blank lines and leading spaces and tabs are ignored. Lines whose first non-space character is a hash-sign (#) are
comments, and are ignored. Note that comments are not allowed on the same line as cron commands, since they will
be taken to be part of the command. Similarly, comments are not allowed on the same line as environment variable
settings.
An active line in a crontab will be either an environment setting or a cron command. An environ‐ ment setting is of
the form: name = value where the spaces around the equal-sign (=) are optional, and any subsequent non-leading
spaces in value will be part of the value assigned to name. The value string may be placed in quotes (single or
double, but matching) to preserve leading or trailing blanks. The value string is not parsed for environmental
substitutions, thus lines like: PATH = $HOME/bin:$PATH will not work as you might expect.
Several environment variables are set up automatically by the cron daemon. SHELL is set to /bin/sh, and
LOGNAME and HOME are set from the /etc/passwd line of the crontab's owner. PATH is set to "/usr/bin:/bin".
HOME, SHELL, and PATH may be overridden by settings in the crontab; LOGNAME is the user that the job is
running from, and may not be changed. Another note: the LOGNAME variable is sometimes called USER on BSD
systems... on these systems, USER will be set also.
In addition to LOGNAME, HOME, and SHELL, cron will look at MAILTO if it has any reason to send mail as a
result of running commands in "this" crontab. If MAILTO is defined (and non-empty), mail is sent to the user so
named. If MAILTO is defined but empty (MAILTO=""), no mail will be sent. Otherwise mail is sent to the owner of
the crontab.
Document1
Enoch E. Damson
Page 4 of 6
2440:145 Operating Systems
Study Guide #2
The University of Akron
Summit College
Dept of Business Technology
Computer Information Systems
If the /etc/cron.allow file exists, then you must be listed therein in order to be allowed to use this command. If the
/etc/cron.allow file does not exist but the /etc/cron.deny file does exist, then you must not be listed in the
/etc/cron.deny file in order to use this command. If neither of these files exists, then depending on site-dependent
configuration parameters, only the super user will be allowed to use this command, or all users will be able to use
this command. For standard Debian systems, all users may use this command.
If the -u option is given, it specifies the name of the user whose crontab is to be tweaked. If this option is not given,
crontab examines "your" crontab, i.e., the crontab of the person executing the command. Note that su can confuse
crontab and that if you are running inside of su you should always use the -u option for safety's sake.
The first form of this command is used to install a new crontab from some named file or standard input if the
pseudo-filename ``-'' is given.
The -l option causes the current crontab to be displayed on standard output.
The -r option causes the current crontab to be removed.
The -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment
variables. The specified editor must edit the file in place; any editor that unlinks the file and recreates it cannot be
used. After you exit from the editor, the modified crontab will be installed automatically.
On the Debian GNU/Linux system, cron supports the pam_env module, and loads the environment specified by
/etc/security/pam_env.conf. However, the PAM setting do NOT override the settings described above nor any
settings in the crontab file itself. Note in particular that if you want a PATH other than "/usr/bin:/bin", you will need
to set it in the crontab file.
By default, cron will send mail using the mail "Content-Type:" header of "text/plain" with the "charset=" parameter
set to the charmap / codeset of the locale in which crond is started up - ie. either the default system locale, if no
LC_* environment variables are set, or the locale specified by the LC_* environment variables ( see locale(7)). You
can use different character encodings for mailed cron job output by setting the CONTENT_TYPE and
CONTENT_TRANSFER_ENCODING variables in crontabs, to the correct values of the mail headers of those
names.
Crontab Format
Commands are executed by cron when the minute, hour, and month of year fields match the current time, and when
at least one of the two day fields (day of month, or day of week) match the current time.
A field may be an asterisk (*), which always stands for "first-last".
Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive.
For example, 8-11 for an "hours" entry specifies execution at hours 8, 9, 10 and 11.
Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: "1,2,5,9", "0-4,8-12".
Step values can be used in conjunction with ranges. Following a range with "/" specifies skips of the number's value
through the range. For example, "0-23/2" can be used in the hours field to specify command execution every other
hour (the alternative in the V7 standard is "0,2,4,6,8,10,12,14,16,18,20,22"). Steps are also permitted after an
asterisk, so if you want to say "every two hours", just use "*/2".
Names can also be used for the "month" and "day of week" fields. Use the first three letters of the particular day or
month (case doesn't matter). Ranges or lists of names are not allowed.
The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to
a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab
file. Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters,
and all data after the first % will be sent to the command as standard input. There is no way to split a single
command line onto multiple lines, like the shell's trailing "\".
Note: The day of a command's execution can be specified by two fields - day of month, and day of week. If both
fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example,
"30 4 1,15 * 5" would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.
Document1
Enoch E. Damson
Page 5 of 6
2440:145 Operating Systems
Study Guide #2
The University of Akron
Summit College
Dept of Business Technology
Computer Information Systems
Instead of the first five fields, one of eight special strings may appear:
string
-----@reboot
@yearly
@annually
@monthly
@weekly
@daily
@midnight
@hourly
meaning
------Run once, at startup.
Run once a year, "0 0 1 1 *".
(same as @yearly)
Run once a month, "0 0 1 * *".
Run once a week, "0 0 * * 0".
Run once a day, "0 0 * * *".
(same as @daily)
Run once an hour, "0 * * * *".
An example of crontab format with commented fields is as follows:
# Minute
Hour
Day of Month
Month
Command
# (0-59) (0-23)
(1-31)
(1-12 or Jan-Dec)
0
2
12
*
/usr/bin/find
Day of Week
(0-6 or Sun-Sat)
0,6
This line executes the "find" command at 2AM on the 12th of every month that a Sunday or Saturday falls on.
Examples
Here are some more examples of crontab lines. Use the command "crontab -e" to edit your crontab file.
This line executes the "ping" command every minute of every hour of every day of every month. The standard
output is redirected to dev null so we will get no e-mail but will allow the standard error to be sent as a e-mail. If
you want no e-mail ever change the command line to "/sbin/ping -c 1 192.168.0.1 > /dev/null 2>&1".
*
*
/dev/null
*
*
*
/sbin/ping -c 1 192.168.0.1 >
This line executes the "ping" and the "ls" command every 12am and 12pm on the 1st day of every 2nd month. It also
puts the output of the commands into the log file /var/log/cronrun.
0 0,12 1 */2 * /sbin/ping -c 192.168.0.1; ls -la >>/var/log/cronrun
This line executes the disk usage command to get the directory sizes every 2am on the 1st through the 10th of each
month. E-mail is sent to the email addresses specified with the MAILTO line. The PATH is also set to something
different.
PATH=/usr/local/sbin:/usr/local/bin:/home/user1/bin
MAILTO=user1@nowhere.org,user2@somewhere.org
0 2 1-10 * * du -h --max-depth=1 /
This line is and example of running a cron job every month, on Mondays whose dates are between 15-21. This
means the third Monday only of the month at 4 a.m.
0 4 15-21 * 1 /command
Document1
Enoch E. Damson
Page 6 of 6
Download