Monday, October 04, 2004

Crontab

crontab
Auto shutdown with crontab
Check the date and time on your machine (on every reboot and esp. when you are leaving the computer on all night). date
If necessary set the date on your machine. Note: See setdate.html to have the date set at boot. syn: date MMDDHHMM[YY]YY[.SS] (mon,day,hour,min,year,sec)
ex: date 090911371998.55
ex: date 0910180298
As root, use only "crontab -e" to edit the crontab file. crontab -e
Note: The crontab utility defaults to the vi editor. Search vi and learn a few basic commands. Esc : q! [Enter] Quit without saving.
Esc : wq [Enter] Save and quit.
i Insert mode (enter text)
[Enter] Starts a new line at cursor position.
Esc Command mode (for delete, etc.)
x Delete a letter
dd Delete the entire line.
Note: While it is a good idea to get used to using vi, you can invoke your own editor from the command line by changing the EDITOR environment variable from the command prompt. For instance # export EDITOR=emacs
or
# export EDITOR=me
Return to vi with
# export EDITOR=vi
In insert mode add the following line: 45 7 * * * /sbin/shutdown -r 2 Morning Reboot
Check root's mail the next time you log in to see if the shutdown worked.
Basic crontabCrontab documentation
crontab Will get you help
crontab [-u user] -e Edit file for user
ex: crontab -u horn -e [root edits crontab for horn]
ex: crontab -e [horn edits crontab for horn]
Here is the format:

MIN HOUR DAY(ofmonth) MONTH DAY(ofweek) COMMAND
ex: 05 7 * * * /root/cronjobs.pl # 7:05 am every day
ex: 45 7 * * * /sbin/shutdown -r 2 Morning Reboot
ex: 10 2 * * * /root/cronshutdown.pl
ex: 0 2 * * mon-wed /root/bin/motd.pl # 2:00am mon, tue, wed
Exercises:
Try the following to see what message you get in root's mail.
ex: 5,10,15,20,25,30,35,40,45,50,55 * * * * /sbin/shutdown -k 1 Mock Shutdown
Use crontab in a combination with a script to get selective days.
ex: 10 * * * * /home/horn/crontime.pl
Now view the contents of crontime.pl
Users crontab
/etc/cron.allow [Without these all users can use crontab.]
/etc/cron.deny
ls /var/spool/cron [To see which users have set a crontab.]
Linux Cron
Linux offers a convenient way to do cron. It supplies the following
directories in /etc
cron.daily
cron.hourly
cron.monthly
cron.weekly
Just pop an executable (script or program) into one of these subdirectories.
Don't touch /etc/crontab. Here is what it looks like:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 1 * * * root run-parts /etc/cron.daily
02 2 * * 0 root run-parts /etc/cron.weekly
02 3 1 * * root run-parts /etc/cron.monthly
Note:
Some cron commands are read from /var/spool/cron/root