I have a Perl script that uses NET::IMAPClient and MIME::Parser which simply reads new emails from an IMAP server and saves any attachments to disk.

My question is: what is the best way to run this script as soon as there's new email? I can use cron to periodically run the script, I could check every few seconds even, but I suspect there is a better way.

Ideally I would act upon the receiving of an email immediately, like a system event. What are common software and techniques to achieve this? I'm using a Debian system.

  • What mail server is this using? Sendmail, postfix? – slm Jun 25 '14 at 17:06
  • @sim the script connects to imap.gmail.com:993 – BryanK Jun 25 '14 at 19:08

In general you keep checking for e-mail unless, as mentioned by @JoelDavis, the server can be extended with some push command.

Further if your e-mail server supports it one can make use of the IDLE extension for IMAP4:

The IMAPClient has native support for IDLE:


In general, programs that transfer data operate by "poll" or "push." Unless the IMAP server can be modified to push the data to the server with the perl script on it, you can only periodically poll for events.

One alternative to cron would be to just daemonize the perl script so that it just goes to sleep after each check. This saves you an execve and depending on how often you poll, you might even save the time it takes to establish the TCP connection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.