## An Introduction 
  ## Eric De La Cruz Lugo <> 
 
  What???  It�s 5:00 PM!  You have a date with someone very important at
  5:15 PM!  Before you go, you have several things to do; send e-mail to
  20 people (telling them the same thing (Your cat had kittens!),
  transfer files from 6 (six) FTP sites, check when and where a user is
  connecting to your system, make a back up and compile that new device
  support in your machine's kernel and if that wasn't enough, your
  provider sent you an e-mail telling you that the connection will end
  in 45 minutes and will be back on line at 8:30 PM for routine
  maintenance of their servers.  What you can do? 
  If you where a Windows user, then you would have to give up and wait 
  until the next day, because after your date you'd probably want some
  sleep before connecting to the net again (after all you are only
  human.. aren't you?). 
  But you are not a Windows user, you are a FreeBSD user and that single
  fact make you capable to do things that a mere mortal (read 
  Windows/DOS User) can�t do. 
 
  In FreeBSD (and other Unix-like OS as well) a cool command exists that
  lets you run instructions (like a shell script) at a given time.  The
  name of this command is crontab. 
  What is it and how does it work?
  The Cron daemon
  As the FreeBSD System Manager's Manual says, cron is "a daemon
  that executes scheduled commands". 
  	"Cron searches /var/cron/tabs for crontab files which are 
	named after accounts in /etc/passwd; crontabs found are loaded
	into memory.  Cron also searches for /etc/crontab which is in 
	a different format.  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)".
   
  And here is where all begins, the key to the solution is the crontab
  file.  This file has a special format, as we will see later, and it
  can be named after the user name running it. 
 
  A crontab file contains instructions to the cron daemon in the
  general form of "run this command at this time on this date".  Each
  user, as we saw before, has their own crontab, and commands in any
  given crontab will be executed as the user who owns the crontab.
  There are also some restrictions using the crontab command. 
  If the "/var/cron/allow" file exists on the system, then you must be 
  listed there in order to be allowed to use this command.  If the
  allow file does not exist but the "/var/cron/deny" file does, then you
  must not be listed in that 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 it. 
  In this case, we assume that all users in your system are able to use
  the crontab command.  The format of this file is shown below. 
  Each line has five time and date fields, the time and date fields
  are: 
  	field			allowed values
	-----			--------------
	minute			0-59
	hour			0-23
	day of month		1-31
	month			1-12 (or names, see below)
	day of week		0-7 (0 or 7 is Sun (Sunday), or use names)
   
  A field may be an asterisk (*), which always stands for "first-last". 
  A crontab file example is given below, the # character is taken as a 
  comment: 
  	# run a shell script named "daily.job" one minute after midnight,
	# every day -- output mailed to eric
	1 0 * * *       $HOME/bin/daily.job 
	# run another shell script named monthly at 1:25pm on the first of
	# every month -- output mailed to eric
	25 13 1 * *     $HOME/bin/monthly
	# run at 10 pm on Monday, send e-mail to my friends
	0 22 * * 1      $home/bin/mail.to.friends
	# Check at 9:05 AM every Monday what users are logged in the 
	# "itesocci.gdl.iteso.mx" machine, output mailed to eric.
	5 9 * * mon     finger @itesocci.gdl.iteso.mx 
	# Make a FTP session at 11:30 PM following the commands stored in the
	# "mp3" file keep in sync every day.
	30 23 * * * ftp -n -v < /home/maestros/ar/eric/bin/mp3
   
  In the FTP session as shown above the file "mp3" contains ftp commands: 
  	iteso.mx:/home/maestros/ar/eric/bin% cat mp3  
	open 138.232.8.4  
	user ptagh share
	lcd /home/maestros/ar/eric/mp3
	hash
	bin
	prompt
	cd unsorted  
	mget "*.mp3"
	bye
	iteso.mx:/home/maestros/ar/eric/bin% 
   
  As you can see the username and password are readable, that�s why this 
  "mp3" file most be readable only by the owner (use the apropiate 
  parameters with the chmod command (check the manual: "man chmod"). 
  	iteso.mx:/home/maestros/ar/eric/bin%chmod 600 mp3
   
  The command above, makes the file "mp3" readable and writeable only 
  for the owner. 
  In the e-mail, section as shown above, we have a shell script named 
  "mail.to.friends", this shell script is shown below: 
   
	#!/bin/sh
	mail -s "My cat have kitties!!" [email protected], 
	[email protected], [email protected], [email protected] <
	cats
   
  In order to run this script from our crontab file we need to make it 
  executable, we do this typing something like: 
  	iteso.mx:/home/maestros/ar/eric/bin%chmod a+x mail.to.friends
   
  Now that we know how the cron daemond and the crontab command works, 
  we can create a crontab file say "eric" (in your case put your account
  name or anyting you want) with your favorite editor (vi, pico, joe,
  emacs...etc).  In this file we can put the example file shown above
  (of course we assume that you can make your own changes on this file
  in order to do what you really want), and after that, only type: 
  	iteso.mx:/home/maestros/ar/eric/bin%crontab eric
   
 
  And that�s it!  Now the cron daemond will check every minute for the
  time it will execute the commands, and the output will be mailed to
  (in this case) the user "eric". 
  If you want to stop a crontab just type: 
  	iteso.mx:/home/maestros/ar/eric/bin%crontab -r
   
  I hope this article helps those who want to obtain the maximum of an
  OS like FreeBSD and other Unix like systems. 
 
  After all, automation is suposed to be one of the best features for a 
  serious OS, isn�t it? 
  More references
  I strongly suggest that you read the manuals for cron and crontab.
  You can also check: 
  
  Have a good time "croning" ;) 
     
  -Eric 
  Eric De La Cruz Lugo, works for the "CONSTRUCTORA KEPLER S.A. DE C.V."
  a Construction company in Mexico.  Right know he is working on a 
  project in Merida, Yucatan, building a Power Plant.  He is a loyal
  FreeBSD user and admin since 1993 at ITESO University (Guadalajara, 
  Jalisco, Mexico).  He can be reached at
  <>
  or <>. 
  Return to Issue #5 
   |