The agony
Crontab can be a little tricky, but if you follow these simple steps.... OK. They may not seem so simple to everyone, but really, just follow along, & you'll have all sorts of annoying tasks lifted from your daily reutine all being done automatically with little or no notice on your part.
Gotta tell ya, vi is, in my opinion, highly over rated. Sure, it does a lot, & a coder can set it up to do lots of things automatically. But talk about bloat -- & all the esoteric commands! Call me a weenie, but Pico's so much simpler, does everything I've ever needed (even spell check) & more that I use nothing else. I don't even use any office products -- unless I really have to. So, I say, don't use vi unless you really want the hassles.
Now, by entering man 5 crontab at a prompt ($ man 5 crontab), the first sentence tells us this:
"A crontab file contains instructions to the cron(8) daemon of the general form: 'run this command at this time on this date'."
As the name implies, cron runs applications at specific times. To set this up, you use crontab. At a prompt, enter crontab -e ($ crontab -e -- the -e is necessary to tell crontab that you want to edit your existing crontab -- if you don't have one yet, crontab creates one in your home directory).
There's a safer way to do it too: Either enter $ touch crontab-temp or call it anything you'd like, then open the file in any text editor. Here you'll enter this nifty guiding info below:
#minute (0-59), #| hour (0-23), #| | day of the month (1-31), #| | | month of the year (1-12), #| | | | day of the week (0-6 with 0=Sunday). #| | | | | commands MAILTO="> /dev/null"
This info, apart from the last line, is simply so you remember what goes where. The last line is so that you won't get endless system e-mails telling you that cron is doing what you've set it up to do.
Opening crontab with the -e option will open it in the vi editor. As implied above, the vi editor is rather intimidating, but all one needs to know about it is the Insert command (i), delete line (dd), & the Save & Exit (Esc : x) commands. So, type crontab -e & open it up. It will likely be empty except for MAILTO=""^[ at the top followed by ~'s all the way down the page.
Pico, which comes with Pine is clean & simple -- don't let any ubergeek tell you otherwise. I'll tell you shortly how to make life easier -- & safer -- using pico.
Here's an example crontab I'd created:
MAILTO="> /dev/null" 1,8,16,24,32,40,48,56 * * * * /usr/bin/fetchmail -a 0-59 * * * * /usr/games/fortune > ~/.fortune; cat ~/.sig ~/.fortune > ~/.signature * 4 1,7,14,21,28 * * cat /dev/null > .procmail-log ~ ~ ~ ~ ~ "/tmp/crontab.5044" 2 lines, 112 characters
Before I explain what's happening, I gotta tell ya, I'm hardly an expert at setting cron jobs (so, you might check out Nuintari's page for a better explanation: (http://www.erg-mad.com/linux/tricks/cron.shtml), but basically, it's composed of five asterisks, followed by the path to the application you want to run, then any options that application uses, & any other things you want to happen behind the scenes.
But first, "MAILTO="> /dev/null"" is very, very important. This tells the cron daemon to send the e-mail it'll send you to send it to /dev/null (a nonexistant system directory) instead, so that it's essentially being deleted. Otherwise, you'll get an e-mail from cron each & every single time it does something. Trust me, you don't want that.
The next line is for fetchmail with the -a (all) switch. In the example, I have it set to check for mail every eight minutes. A full explanation of how to enter the numbers is below
The asterisks following are for hour, day, month, & year. This example means that every ten minutes of every hour of every day of every month of every year, cron will "wake up" fetchmail to check my mail server & fetchmail will download anything new it finds there (of course, I'm connected to the internet via DSL, & a modem connection would need some other configuration -- hey, I've never connected a Linux box to the 'Net with an analogue modem, so I dunno, but check out Scott's Configuring Pine page -- he may get you connected).
My next entry does something really cool, & does it in a kind of short-hand. Unlike the fetchmail line, this one is running not only every minute -- by 0-59 instead of entering 1,2,3, etc all the way to 60 -- it's also a set of complex commands. First, it runs the program fortune, but instead of returning the fortune in a terminal window, it then cats (writes) it to a file named ~/.sig (which is actually /home/username/.sig), then takes that date & puts it in another file name .fortune (also in the user's home directory), & finally writes it to a third file named .signature. Yeah, sounds like a lot of wasted effort, but this was the only way I could get fortune to write to the .signature file so pine would put a new signature from fortune into every e-mail I wrote. There may be an easier way to make it work, but as this does work, I no longer mess with it.
So to further explain the numbers on the second line, instead of all the cool things I did, for the first asterisk, I've entered those numbers on the second line, "0-59", representing every minute in every (in this case) hour of the day. But, on the third line, I've left the first asterisk to tell cron that (again, in this case) I only want this job performed every 4 hour, or in crong-speak, at 4.00am. Following this, the third asterisk -- for day of the month -- I have entered the numbers "1 7 14 21 28" & have separated them by commas to tell cron to only perform this on the 1st, 7th, 14th, 21st, & the 28th days of the month.
The third entry is just an example of something else you might want to do. My .procmail log file gets huge because I subscribe to numerous mailing lists & get tons of spam. I also had had a domain for many years & would get tons more spam because of that. So, to get rid of this growing file, I would delete it periodically. In this case, I don't do it on a minute basis, but rather every week & a specific time. So, the minute spot is an astrisk, then, at 4.00am on the 1st, 7th, 14th, 21st, & 28th of the month, cron will delete (send the file to /dev/null) the .procmail-log in my home directory.
After you enter the info, to save & exit with vi (trust me, it took me a long time to find this), press your Esc key, then type a : (that's a full colon), type x & press Enter. If there's anything not set properly, like a mis-type or spaces between the commas, crontab will tell you.
In addition, you'll find that you're probably still getting mail from cron. Even if you don't, you'll do well to do something more. Open a terminal window, enter su, & type the root password (if you're not in X, ignore that, or if you've opened X after logging in as root, just open the terminal window), type pico /etc/crontab & press Enter. The crontab you've just opened is the system crontab & you'll probably find that there's a line near the top reading MAILTO=""^[. Remove the ^[ from it, hold your CTRL key & press x to save & close. Pico will ask if you want to save, press y & then Enter to save with the name crontab. You're done.
I've been told that there's a security issue with this /etc/crontab & that it may be best to delete it & any other crontab on the system but one. The one you keep could be /root/crontab or /home/your-user-name/crontab. If you choose to save the one in a user space, remember that some jobs may require super user permissions & you should decide appropriately. I live dangerously, leave the one in /etc, create one for me & another for root. I'm used to being called stupid, but you may not be. So think about what you really wanna do.
Now, the easy way: Copy any existing crontab file you can find
on your system to your home directoy with a name like "crontab-temp", or anything you like.
Open that one in pico, edit it to only show what you want -- keeping the info at the top, of
course, etc. Then, save & close it with ctrl-x, &, at the prompt, enter "crontab crontab-temp"
($ crontab crontab-temp
Finally, just for fun, I'm including another crontab I'd used some time back with lots of backup entries. I forget why I was backing up so much -- maybe I was losing data -- anyway, it'll get you a better idea of how to use crontab. You can copy it directly to your system & edit it to suit your own needs:
#minute (0-59), #| hour (0-23), #| | day of the month (1-31), #| | | month of the year (1-12), #| | | | day of the week (0-6 with 0=Sunday). #| | | | | commands MAILTO="> /dev/null" 1,10,20,30,40,50 * * * * /usr/bin/fetchmail -a 0-59 * * * * /usr/games/fortune > ~/.fortune; cat ~/.sig ~/.fortune > ~/.signature * 2 * * * rm /home/meph/mail/fetchlog * 4 1,7,14,21,28 * * cat /dev/null > .procmail-log 1 22 * * * cp /home/meph/mbox /zip/mailspool 1 3 * * * cp /home/meph/mbox /zip/mailspool 2 3 * * * cp /home/meph/.addressbook* /zip/ 3 3 * * * cp /home/meph/.bash* /zip/ 4 3 * * * cp /home/meph/.fetchmailrc /zip/ 5 3 * * * cp /home/meph/.fortune /zip/ 6 3 * * * cp /home/meph/.forward /zip/ 7 3 * * * cp /home/meph/.mailcap /zip/ 8 3 * * * cp -Rf /home/meph/.mailnotify /zip/ 9 3 * * * cp /home/meph/.mime.types /zip/ 10 3 * * * cp /home/meph/.pinerc /zip/ 11 3 * * * cp -Rf /home/meph/.procmail /zip/ 12 3 * * * cp /home/meph/.procmailrc /zip/ 13 3 * * * cp /home/meph/.sig* /zip/ 14 3 * * * cp /home/meph/.svgalibrc /zip/ 15 3 * * * cp /home/meph/.w3m-m17n/bookmark.html /zip/ 16 3 * * * cp /home/meph/.w3m-m17n/config /zip/ 17 3 * * * cp /home/meph/.w3m-m17n/cookie /zip/ 18 3 * * * cp /home/meph/.w3m-m17n/history /zip/ 19 3 * * * cp /home/meph/CoverLetter /zip/ 20 3 * * * cp /home/meph/Resume* /zip/If you look closely, you'll notice a couple of things: 1. One of the commands is "cp -Rf /home/meph/.mailnotify /zip/". This one not only copies, but forces the removal of the .mailnotify file. This is really handly for files you don't want growing to massive size. 2. There's no command to zip the backups copied to /zip. I'm sure I had a reason for this, but you could easily add two more lines:
21 3 * * * zip /zip/* /zip/zips/dailyzipofbackups.zip 23 3 * * * rm /zip/*Or something to this effect. I'll check the zip man file to see if there's a way to name the file with the date, so you get different names for each zip file made & deposited in the /zip/zips/ directory. Of course, with the two lines above, you'll still get an error saying that rm cannot remove the directory /zip/zips/, but you don't want to delete that.
My general page
My personal home page
Me, Myself, & the Voices in my Head
Copyright 2004-2006