old-www/LDP/LG/issue66/adam.html

287 lines
17 KiB
HTML

<!--startcut ==============================================-->
<!-- *** BEGIN HTML header *** -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD>
<title>The Weekend Mechanic LG #66</title>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
ALINK="#FF0000">
<!-- *** END HTML header *** -->
<CENTER>
<A HREF="http://www.linuxgazette.com/">
<H1><IMG ALT="LINUX GAZETTE" SRC="../gx/lglogo.png"
WIDTH="600" HEIGHT="124" border="0"></H1></A>
<!-- *** BEGIN navbar *** -->
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="lg_tips66.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue66/adam.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../faq/index.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="collinge.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
<!-- *** END navbar *** -->
<P>
</CENTER>
<!--endcut ============================================================-->
<H4 ALIGN="center">
"Linux Gazette...<I>making Linux just a little more fun!</I>"
</H4>
<P> <HR> <P>
<!--===================================================================-->
<center>
<H1><font color="maroon">The Weekend Mechanic</font></H1>
<H4>By <a href="mailto:n6tadam@users.purbeck.dorset.sch.uk">Thomas Adam</a></H4>
</center>
<P> <HR> <P>
<!-- END header -->
<H1><CENTER>Welcome to the new Linux Weekend Mechanic!</CENTER></H1>
<H2>Table of Contents</H2>
<UL>
<LI><A HREF="#wmdesc">What exactly is the Weekend Mechanic?</A>
<LI><A HREF="#customshell">Customising the shell environment</A>
<LI><A HREF="#crontab">Setting up and using crontab</A>
<LI><A HREF="#closing">Closing Time</A>
</LI>
</UL>
<HR>
<!-- Section one - what is the LWM-->
<A NAME="wmdesc"><H2>What exactly is the Weekend Mechanic?</H2></A>
<P>Welcome to this months new feature....<B>The Weekend Mechanic</B>. Actually,
for those of you who have been avid readers since LG's initial release, you'll
realise that this column used to be written by John M Fisk in 1996-1998 and so
it is not that new. However, I thought it would be nice to re-introduce this as
a regular feature.</P>
<P>The Weekend Mechanic will draw together my experiences of Linux and the problems that I have had to solve either at home or at school each month. So, The Weekend Mechanic will concentrate on the following:</P>
<P>
<UL>
<LI>General Linux News
<LI>Shell Programming
<LI>Shell Customisations
<LI>Sed, Awk, hints
<LI>Program Reviews
<LI>And anything else.....
</LI>
</UL>
</P>
<P>So, with that in mind, lets begin this months fixing and tinkering session......</P>
<!-- ** END SECTION ONE ** -->
<HR>
<!-- ** BEGIN "CustomShell" -->
<A NAME="customshell"><H2>Customising the Shell Environment</H2></A>
<P>I have noticed that more and more people when using Linux tend solely to rely on the GUI, hoping in vein that they do not have to type in any commands in fear of deleting their work, making a fatal mistake, etc. Although the only real threat of this happening is if you are logged in as "root", I find that people are still wary!! However, there will come a time regardless when typing in commands will be a necessity and it is important that your shell environment is customised so that you can complete your tasks with ease. This article will show you how to customise the login shell so that features such as <I>Aliases, editors, shells</I>, etc can work in the way that you want them to.</P>
<P>Firstly, we should make sure that you have an appropriate editor installed. There are many console editors to choose from, such as: emacs, joe, jed, pico, vi. Once you have found an editor that you like (I happen to use both Pico and Jed) then you can tell the shell to use. Some programs such as Cron (as we shall see later on..) rely on the shell having an editor set up so that you can edit the crontab.</P>
<P>There are two files that we shall be concentrating on. They are located in
your home directory as: <B>.bashrc</B> and <B>.bash_profile</B>. In my .bashrc
file, I find that it begins with the following:</P>
<PRE>
# User specific aliases and functions
alias ls='ls -o --color=auto'
alias cad='cat /var/squidGuard/db/blacklist/adverts'
alias cc='cd /mnt/cdrom/Mandrake/RPMS'
alias mail='mail -v'
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias d='ls'
alias s='cd ..'
alias p='cd -'
</PRE>
<P>Aliases are useful, especially if you find yourself typing out a command that has a deep directory listing. For example, if you found yourself having to keep typing in the command <PRE>cd /var/spool/users/home/mail/root/sun</PRE>to save all that typing you can specify a "shortcut" word that automatically does just that. Cool eh?</P>
<P>So to tell the shell that you want to use the word "checkmail" to do the command <PRE>cd /var/spool/users/home/mail/root/sun</PRE>you would add to the list:</P>
<P><PRE>alias checkmail='cd /var/spool/users/home/mail/root/sun'</PRE> Then you could type the alias <B>checkmail</B> and hey presto....it works!!</P>
<P>Of course many people like to issue aliases to accommodate their typographical errors; i.e.,
<PRE>
alias eamcs='emacs'
alias emcas='emacs'
</PRE>
Personally I think this is a bad idea, and you should learn to type more accurately!</P>
<BR>
<P>The next section we shall look at is how to tell the shell which programs to run when it is suitable to run them. In my <B>.bash_profile</B> file I have among the following:</P>
<PRE>
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin
ENV=$HOME/.bashrc
USERNAME="root"
export USERNAME ENV PATH
</PRE>
<P>This is the section that we shall be concentrating upon setting these variables. Common variables that have not been set are ones like "EDITOR" and "MAIL". These variables are common to the user that is currently logged in, meaning that different values can be specifies for each user.
The variable <NOBR><PRE>EDITOR</PRE></NOBR> specifies the editor to use when editing files. This variable is usually called from programs such as <I>Pine</I> and <I>Cron</I>, but it can be very useful when writing shell scripts.</P>
<P>To set the variable, one has to add it to the "export" list, like this:<BR><BR><PRE>export USERNAME ENV PATH EDITOR</PRE></P>
<P>Exporting a variable releases it into the environment, rather than keeping it within a single program. Exporting is done, so that many different programs can use the same variable name with the same value, get it :-).</P>
<P>Once added to the export list, save the file, and exit your editor. So, now that we have defined a new variable, the next thing to do is to tell Bash, that it is there. To do this, you must "source" the file. This is a bash builtin that re-reads the file. You can either type this in, in one of two ways. Either you can specify <BR><PRE>source filename</PRE>or you can specify a "." thus:</P>
<PRE>. filename</PRE>
<P>And that will then active your new added variable. Well, thats it for this section....</P>
<!-- ** END "customshell" ** -->
<HR>
<!-- ** BEGIN "crontab" ** -->
<A NAME="crontab"><H2>Setting up and Using Crontab</H2></A>
<P>Do you ever find yourself repeating common tasks throughout the day, and wished that there was some sort of program that would automate it all for you? Well, look no further, Mr. Cron is here :-)</P>
<P>Cron is a scheduling program, and even more specifically it is known as a <I>daemon</I>. By daemon, I do not mean that it is a nasty creature with two horns!! A daemon is simply a program that runs in the background waiting for instructions. When it receives them, it executes them and when it has finished, it goes dormant again.</P>
<P>Cron is usually started when you switch to one of your run-levels. However, just to make sure it has started, issue the following command:</P>
<PRE>
ps ax | grep crond
</PRE>
<P>If you get a response similar to:</P>
<PRE>
root 311 0.0 0.7 1284 112 ? S Dec24 0:00 crond
root 8606 4.0 2.6 1148 388 tty2 S 12:47 0:00 grep crond
</PRE>
<P>Then cron has started, and you are ready to use it. If you don't get "crond" returned, then you must start the daemon, by typing <PRE>crond</PRE></P>
<P>Cron is particularly useful when you find yourself needing to run backup and general maintenance programs. To tell cron when to run a program, you have to fill out several fields. Each separate program that is scheduled via cron is put into a file known as a <I>crontab</I> file. The fields are defined as the following:</P>
<PRE>
Min Hour DOM Month DOW User Cmd
</PRE>
<P>And a description of their input values are summarized in the table below:</P>
<TABLE CELLPADDING="5" BORDER="1" WIDTH=100%>
<TH>FIELD</TH>
<TH>DESCRIPTION</TH>
<TR>
<TD ALIGN="center"><B>Min</B></TD>
<TD ALIGN="left">Specifies the minute on or past the hour. Values range from 0 to 59.</TD>
</TR>
<TR>
<TD ALIGN="center"><B>Hour</B></TD>
<TD ALIGN="left">Specifies the hour (Military style) that the script should run at. The range is from 0 to 23 where "0" is Midnight</TD>
</TR>
<TR>
<TD ALIGN="center"><B>DOM</B></TD>
<TD ALIGN="left">This is the Day of Month, that you want the command run on, e.g. to run a command on the 23th of each month, the DOM would be 23</TD>
</TR>
<TR>
<TD ALIGN="center"><B>Month</B></TD>
<TD ALIGN="left">Specifies the month to run the script in. Values range from 1 to 12, where "1" is January and "12" is December. Or it can be specified using the first three letters of the month, e.g. May</TD>
</TR>
<TR>
<TD ALIGN="center"><B>DOW</B></TD>
<TD ALIGN="left">Specifies the day of the week, either as a numeric value of 0 to 7 (0 and 7 are both Sunday) or as the name of the week (using first three letters only), e.g. Mon</TD>
</TR>
<TD ALIGN="center"><B>User</B></TD>
<TD ALIGN="left">Indicates who is running the command</TD>
</TR>
<TR>
<TD ALIGN="center"><B>Cmd</B></TD>
<TD ALIGN="left">The path and name of the script/program to be run</TD>
</TR>
</TABLE>
<P>You can use a "*" (without the quotes) in any of the time fields to mean
"every minute", "every hour", etc.
<P>So, with the above descriptions in mind, the following examples are all valid:</P>
<PRE>
01 * * * * root /usr/bin/script <B>"This command is run at one min past every hour"</B>
17 8 * * * root /bin/mail <B>"This command is run daily at 8:17 am"</B>
17 20 * * * root /usr/bin/fetch <B>"This command is run daily at 8:17 pm"</B>
00 4 * * 0 root /bin/qweb <B>"This command is run at 4 am every Sunday"</B>
* 4 * * Sun root /usr/bin/file2rpm <B>"This command is run at 4 am every Sunday"</B>
42 4 1 * * root /usr/bin/squidlog <B>"This command is run 4:42 am every 1st of the month"</B>
01 * 19 07 * root /usr/bin/xman <B>"This command is run hourly on the 19th of July"</B>
</PRE>
<P>See how easy it is? :-). Cron also accepts more sophisticated time
specifications: run "man 5 crontab" for an explanation of these.
<P>
Of course this is all very well, but I have not told you where to put any of your cron entries. So.........hang on there, reader.</P>
<P>The most common version of cron installed on linux systems is "vixie-cron", and so in the "/etc" folder should be a file called "crontab". If you have specified the environment variable EDITOR (see the above section) then you can simply type in:</P>
<PRE>
crontab -e
</PRE>
<P>And that will load the file into your text editor
<P>If you did not open it using the above command, then open it using a text editor of your choice and you should find something that looks like the following</P>
<PRE>
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root@grangedairy.linux
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
</PRE>
<P>The SHELL variable indicates the current shell that we are using</P>
<P>The PATH indicates the path to the most common programs</P>
<P>The MAILTO option indicates to whom the output of the cron result (i.e. whether it has worked or not) and any output from the program is to be mailed. If you find that it is annoying, then you can delete this variable.</P>
<P>The section below "#runparts" is supposed to work so that in the folder "/etc/cron.daily" for example, whatever script is in there gets executed daily. However, for some strange reason, it has never worked well for me, and I have since found it easier to specify my own cron list.</P>
<P> So, to add the above examples to our crontab, it is just a matter of <I>copying</I> and <I>pasting</I> them in:</P>
<PRE>
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root@grangedairy.linux
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
#Custom Crontabs -- Put in by Thomas Adam
01 * * * * root /usr/bin/script
17 8 * * * root /bin/mail
17 20 * * * root /usr/bin/fetch
00 4 * * 0 root /bin/qweb
* 4 * * Sun root /usr/bin/file2rpm
42 4 1 * * root /usr/bin/squidlog
01 * 19 07 * root /usr/bin/xman
</PRE>
<P>Then save the file. Now the last thing we have to do is to tell cron that we have edited the file. That is done, with the following command:</P>
<PRE>
crontab -u root /etc/crontab
</PRE>
<P>And thats it...just sit back and wait..... You should find that by now your workload has diminished by about 25% or so!!!</P>
<P>Cron also has the ability of allowing and denying certain users who are allowed to use cron. To implement this, two files called <I>cron.allow</I> and <I>cron.deny</I> have to be created in the folder "/etc".</P>
<P>These files work in the following way. If for example you wanted nobody to have access to cron, then you would add the line "ALL" to the <I>cron.deny</I> file. If you wanted only certain people to use cron then you would add their username to the <I>cron.allow</I> file.</P>
<P>Rather than having to edit the file each time, I find it much easier to use the following command:</P>
<PRE>
cat username &gt;&gt;/etc/cron.allow
</PRE>
<P>Thats all there is to it....have a go and see what you think......!?!</P>
<!-- ** END "crontab" ** -->
<HR>
<!-- ** BEGIN "closing" ** -->
<A NAME="closing"><H2>Closing Time</H2></A>
<P>Well folks, thats it for this month. I had hoped to do more, but unfortunately school work had intervened yet again!! I would really appreciate some feedback, general comments, hints as to articles, etc. Armed with this information I can then go about "making linux just that little more fun" :-)</P>
<P>I am now off to go and teach piano, do some Geography revision (yay) and then maybe continue working on some of my ongoing "bash script projects". After which, I might even be able to get some sleep. who knows?????</P>
<P>In the meantime however, I wish everyone....<B>"happy Linuxing"</B></P>
<!-- ** END "closing" ** -->
<HR>
<H1 ALIGN="center"><FONT COLOR="red">Send me comments....</FONT></H1>
<H2 ALIGN="center">Any comments, suggestions, ideas, etc can be mailed to me by clicking the e-mail address link below:</H2>
<H2 ALIGN="center"><A HREF="mailto:n6tadam@users.purbeck.dorset.sch.uk">Thomas Adam</A></H2>
<!-- *** BEGIN bio *** -->
<!-- *** END bio *** -->
<!-- *** BEGIN copyright *** -->
<P> <hr> <!-- P -->
<H5 ALIGN=center>
Copyright &copy; 2001, Thomas Adam.<BR>
Copying license <A HREF="../copying.html">http://www.linuxgazette.com/copying.html</A><BR>
Published in Issue 66 of <i>Linux Gazette</i>, May 2001</H5>
<!-- *** END copyright *** -->
<!--startcut ==========================================================-->
<HR><P>
<CENTER>
<!-- *** BEGIN navbar *** -->
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="lg_tips66.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue66/adam.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../faq/index.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="collinge.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
<!-- *** END navbar *** -->
</CENTER>
</BODY></HTML>
<!--endcut ============================================================-->