Running Fetchmail Automatically

You can run Fetchmail manually by typing fetchmail at a command prompt. If you have multiple configuration files, you can specify the one you want to use by adding -f and the filename to the command. Manual Fetchmail runs may be appropriate in some situations, but most users want to have Fetchmail automatically retrieve mail on a regular basis. Two ways of doing this exist: You can use Fetchmail's built-in daemon mode or you can run Fetchmail using a timed-run tool such as cron.

The preceding sections and Listing 25.3 describe and demonstrate the use of daemon mode. Add a set daemon interval line to the Fetchmail configuration file, and it will run in the background, checking mail at the specified interval. You can then start Fetchmail automatically in a SysV or local startup script, as described in Chapter 9, "Bypassing Automatic Configurations to Gain Control." In order to run Fetchmail as an ordinary user with the approach, you must use su, as in:

su -c Vusr/bin/fetchmail -f ~/.fetchmailrc' ben

This command launches Fetchmail using the username ben. Alternatively, if your Internet connection isn't up at all times, you could add commands to launch Fetchmail to your PPP dial-up script and to kill Fetchmail to the PPP shutdown script. The interface and monitor options could also be used to keep Fetchmail from trying to retrieve mail when your network connection is down.

Unfortunately, Fetchmail isn't always completely reliable when run in daemon mode. In my experience, the program occasionally hangs, causing it to stop checking mail. In order to avoid this problem, you can launch Fetchmail from a cron job without using daemon mode. For instance, Listing 25.4 shows a crontab file that might run Fetchmail. If you don't already have a crontab for the user who will be running Fetchmail, type Listing 25.4 into a file called crontab, making changes to the username, HOME directory specification, and time listing. When done, type crontab crontab as that user. Linux should enter the script as the user's crontab. If the user already has a crontab file, add the final line of Listing 25.4 to that file and type crontab crontab. If the user already has crontab entries but you can't find them, type crontab -I > crontab as that user to regenerate the original file, which you can then edit.

Listing 25.4: A Sample crontab File for Launching Fetchmail

SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=ben HOME=/home/ben

17,37,57 7-20 * * * /usr/bin/fetchmail > /dev/null

Listing 25.4 causes the /usr/bin/fetchmail program to be run at 17, 37, and 57 minutes past every hour between 7:17 a.m. and 8:57 p.m. This configuration demonstrates one of the advantages of a crontab configuration: You can schedule Fetchmail runs more flexibly than you can with Fetch mail's own daemon mode. Listing 25.4, for instance, doesn't run Fetchmail late at night, minimizing the exposure of your mail passwords when nobody is around to read the retrieved mail. On the other hand, using daemon mode enables you to more easily schedule retrievals from different servers at different intervals by using the interval keyword. You can achieve the same effect using cron, but to do so, you must create two Fetchmail configuration files (say, .fetchmailrc-1 and .fetchmailrc-2) and call them at different intervals using two lines in the crontab file.

Warning If you use a crontab line to run Fetchmail, do not include adaemon line in the

.fetchmailrc file. If you attempt to use both methods, the first time Fetchmail is run from cron, Fetchmail will kick itself into daemon mode and, as a result, error messages will be generated on all subsequent attempts to run the program from cron.

Team LIB

0 0

Post a comment