Cron jobs with mail notification on a Windows box in 5 minutes

In: Uncategorized

28 Jul 2010

So cygwin allows you to port all the great *nix utilities to a Windows environment, and that’s useful because 1) there are some many handy tools and 2) you can write scripts or tasks once and use them everywhere (Unix, Linux, OS X, Windows).

Today, let’s discover how you can get two of the most useful tools (automated tasks and mail notification) installed in less than 5 min.

Since the typical Windows box was not designed as a server, scheduled services/tasks and mail transfer agents are not commonly implemented/installed, but we can quickly fix that.

First, install the exim and cron cygwin package and then run exim-config. You can accept most of the defaults. The only change that you might want to make is to set up a primary hostname. I use “mail.local”. Be sure to add that to your hosts file (%WINDIR%\system32\drivers\etc\hosts), too:

127.0.0.1 mail.local

Next since you want mail to be deliverable for users without accounts, we need to disable the check_local_user option under the localuser router in /etc/exim.conf.

Verify the setup with

$ exim -bt test@mail.local

You should see something like …

test@mail.local
   router = localuser, transport = local_delivery

Now any output from the cron jobs will appear in the mail logs located in /var/spool/mail.

Also note that you can quickly send mail from the command line now without any other tools. Try this:

$ exim -v -odf test@mail.local
This is a test message.

Just end the message with a newline and then type Ctrl+D. Now check /var/spool/mail/test.

Now for cron. Run cron-config accepting the defaults as needed. You can quickly test the setup afterward by editing your crontab.

echo '* * * * * echo hi' | crontab -

In less than a minute (and every minute thereafter until you change it), you should see the output in your mail spool.

And we’re done. Now just set up whatever cron jobs you want on whatever schedule you want. That wasn’t so bad, was it?