Crontab limits


#1

Hello, just a general question here.

I have written a script to handle all incoming emails and forward them to different people. I need to set up a cron task to run the script every x seconds so that emails get sent in a timely manner.

I was planning on setting the cron task for 15 seconds but want to make sure that this shortish time period would not cause a problem. For instance hostgator states a minimum of 15 minutes in their TOS but I could not find any limitations in dreamhost documentations.

Simply, is 15 second interval cron OK? If not, what should I use?

Thanks,
Shawn

Sorry, I’m new to cron, i knew that there were 5 time fields but I thought one was seconds. I am now seeing that there is no seconds field. I read that there are work arounds with sleep commands.


#2

Running a command every 15 seconds on a shared web server is definitely not OK. Every 5 minutes is about the lowest you should consider going.

However, if all you need to do is forward e-mails, you may be able to do that without a script at all! Just use our Mail Filters system, which will let you set up rules to file, forward, and tag email based on its sender, headers, and/or content.


#3

Thanks, it’s a little more complicated than just forwarding emails I’m afraid, it’s all very dynamic pulling address from a database of different lists.

Thanks for the input


#4

How about announcement list? There must be a better way of doing this. Even DH allows it, it is still not a good idea of running a script every 15 seconds.


#5

Is the issue just how often this runs? I can’t imagine someone running something here at DreamHost that’s so critical that someone has to be notified within a minute of something happening. Especially in the middle of the night.


#6

I will not run it every 15 seconds.

here’s what we’re doing so you see why we can not use announcement lists or some other built in dreamhost alternative.

A catch all is set up for our domain. The script checks for new incoming email and based on the original address it was sent to, forwards to either the appropriate person or appropriate group of people. The appropriate people change regularly and are read in from a database of emails and preferences.

When the script runs, if there are no new emails the full extent of processing done is:

include 3 files
read other files from a directory
if there are no files in that directory, exit

So it is efficient, but I will run it every few minutes, also as sdayman pointed out, which I had not thought of, it can run at different intervals for different times. In the night it really can run periodically while in the day it can run more often.

Thanks, I really am open to any ideas or concerns.


#7

Oh – also, if you’re picking up mail sent to your shell user, you can probably use a .procmailrc to trigger processing when an email comes in. See http://wiki.dreamhost.com/Procmail for details.


#8

That looks very promising. I’ll look into running the script from procmail when I get a chance. It would be more effective and efficient than cron.

The emails are for my shell user, they are stored in my Maildir directory. I’ll look into it later, a very quick search did not find how to call a php script from it but now Im at work and can’t look into it further.

Thanks.


#9

Thanks for the info. It is good to learn something everyday :slight_smile:


#10

This really could be it’s own topic but since it’s developing from this discussion Ill post it as a reply.

So I started to fiddle with procmail. I needed to setup a .forward.postfix to forward to procmail and then I set up a .procmailrc

The .procmailrc looks like:

PATH=/bin:/usr/bin:/usr/local/bin
MAILDIR=$HOME/Maildir
LOGFILE=$HOME/log
SHELL=/bin/sh

# Catch-all rule for all unmatched email
:0
| $HOME/email.php

But the log is resulting in:

/home/username/email.php: line 1: ?php: No such file or directory
/home/username/email.php: line 2: require_once: command not found
/home/username/email.php: line 3: require_once: command not found
/home/username/email.php: line 4: require_once: command not found
/home/username/email.php: line 6: syntax error near unexpected token `('

The beginning of email.php looks like:

<?php
require_once "Mail.php";
require_once "Mail/mime.php";
require_once "Mail/mimeDecode.php";

$con = mysql_connect("database","username","password");

It seems to be trying to run the php line by line rather than as php. What am I missing?

Thanks,
Shawn


#11

Figured it all out! Just want to get the solution out there for anyone following or with the same problem.

To fix these errors I just needed a shebang at the beginning of my php code. The very first line before the <?php tag needed to be

#!/usr/local/php5/bin/php

Also, procmail is not necessary. In order to set up the procmail you need to use .forward.postfix to forward to procmail. A simpler solution is to go right from the .forward.postfix to the php. In the .forward.postfix I put:

"| /usr/local/php5/bin/php /home/username/php/myscript.php"

Works more effectively and efficiently than cron!

Here’s a link which explains in much more detail: http://wiki.dreamhost.com/Email_to_Script

Thanks to everyone who helped!