1

I need to set up a weekly email that cycles between 4 variations, eg.

  1. variation a
  2. then b
  3. then c
  4. then d
  5. then back to a
  6. etc

each week.

I'd like to do this using 4 cron tasks (each of which sends an email every 4 weeks), but I'm having trouble staggering them so that each task starts on a different week, rather than having them all send their emails on the first week then having nothing for the next 4 weeks.

My crontab looks like this at the moment:

0  8  *  *  1/4  echo "Variation A" | mail -s "Test email" admin@mydomain.com
0  8  *  *  1/4  echo "Variation B" | mail -s "Test email" admin@mydomain.com
0  8  *  *  1/4  echo "Variation C" | mail -s "Test email" admin@mydomain.com
0  8  *  *  1/4  echo "Variation D" | mail -s "Test email" admin@mydomain.com 

I feel a bit like if I could control when each one sends the email for the first time then I could get it working right?

Can anyone see a better way of doing this? Preferably with a minimal of scripting (I'd like to keep all of the details within the crontab)

2
  • Please use the code-block button on your "code", it's very hard to read.
    – plundra
    Dec 19, 2010 at 23:52
  • 1) Having a /<step> on a single value (not a range) won't do anything. 2) The fifth field is day-of-week, not week-of-month or whatever you were going for.
    – plundra
    Dec 20, 2010 at 0:05

1 Answer 1

0

This is not something you can do with cron alone. You will have to use some sort of script to do this, storing the last variation you sent, and deciding which to send next based on that info. The cron entry would look something like:

0 8 * * 0 somescript.sh

This will execute the script at 8AM every Sunday. The script itself will have to decide which message to send.

You could use something like this:

#!/bin/sh

STATEFILE='emailstate'
VARIATION=`cat emailstate 2>/dev/null`

case "$VARIATION" in
    '4' )
        # send variation 4
        echo 'Sending variation 4'

        VARIATION='1'
        ;;

    '3' )
        # send variation 3
        echo 'Sending variation 3'

        VARIATION='4'
        ;;

    '2' )
        # send variation 2
        echo 'Sending variation 2'

        VARIATION='3'
        ;;

    *   )
        # send variation 1
        echo 'Sending variation 1'

        VARIATION='2'
        ;;
esac

echo $VARIATION > $STATEFILE
3
  • Alternatively, if you choose a date & time to act as the date of the first run, the problem becomes stateless, as you can get the current date & time then figure out where on the cycle you are. The way you have suggested to do it is nice and straightforward though (and probably a better bet). Dec 20, 2010 at 0:49
  • That's a nice cyclic way of doing it (I like the fact that it doesn't rely on dates to get the right variation for the right week). I was thinking of using "date +%s" then calculating the difference between the date of the first email and the current date to work out which week we're currently on (but the maths is a little off-putting) Dec 20, 2010 at 0:50
  • @Mark: That's still a kind of state, it just won't change on each run. I did consider something like that, but I like my current approach better because if the number of variations changes, the behavior is still correct -- the one following the last one will be sent. Date math could cause any variation to be sent after a change, depending on the date of the change.
    – cdhowie
    Dec 20, 2010 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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