March 21st, 2008 by depesz | Tags: , , | 10 comments »
Did it help? If yes - maybe you can help me?

simple brain teaser:

given standard unix crontab what is the longest possible interval between calls to the same program?

for example:

15 */2 * * * /bin/echo x

has interval 2 hours.

what is the longest possible? and how to make it?

  1. 10 comments

  2. Mar 21, 2008

    A year (@yearly).

  3. Mar 21, 2008

    just in case – i know the answer. or at least i think i know.

  4. # cezio
    Mar 21, 2008

    4 years? (29 feb)

  5. Mar 21, 2008

    if i’m right, this is the correct answer:

    perl -MMIME::Base64 -le ‘print decode_base64″MjggeWVhcnMuIGNyb246IDAgMCAyOSAyIDI=”‘

    run this, it will print answer – i “coded” it not to spoil teaser for others.

  6. # Evolic
    Mar 21, 2008

    I don’t understand
    Why you place “2” on year column?
    What it gives you?

  7. # majek
    Mar 21, 2008

    I would guess something like this:
    python -c ‘import base64; print base64.decodestring(“MCAwIDI5IDIgNCAtPiBnaXZlcyAyOCB5ZWFycywgbmV4dDogMjAzNg==\n”)’

    On the other hand remember about leap seconds! Crontab doesn’t support second precision, but if it did: 61st second, monday, 30th june

    I think nobody can tell me whan it would happen πŸ™‚

  8. # yn
    Mar 22, 2008

    python -c “print ‘IyEvdXNyL2Jpbi9weXRob24KCmZyb20gZGF0ZXRpbWUgaW1wb3J0IGRhdGV0aW1lCgptYXhfeWVh\ncnMgPSAwCnByZXZfdHVlMl8yOSA9IHN0YXJ0ID0gMTk4NAoKZm9yIHllYXIgaW4gcmFuZ2UoIHN0\nYXJ0LCAzMDAwICk6Cgl0cnk6CgkJaWYgZGF0ZXRpbWUoIHllYXIsIDIsIDI5ICkud2Vla2RheSgp\nID09IDI6CgkJCXllYXJzID0geWVhciAtIHByZXZfdHVlMl8yOQoJCQlpZiB5ZWFycyA+IG1heF95\nZWFyczoKCQkJCW1heF95ZWFycyA9IHllYXJzCgkJCXByZXZfdHVlMl8yOSA9IHllYXIKCWV4Y2Vw\ndCBWYWx1ZUVycm9yOgoJCXBhc3MKCnByaW50IG1heF95ZWFycwoK’.decode(‘base64’)” | python

  9. # Evolic
    Mar 22, 2008

    I see you liked python coded answers πŸ™‚
    Now I know that last “column” is day of week, not a year
    my mistake 😐
    It changes a lot πŸ˜‰

  10. Mar 22, 2008

    @Evolic:
    tuesday. cron means: every 29th of february, which is tuesday (i could use any day, but tuesday looks nice)

    @majek:
    great – actually – it’s practically the same solution – it shouldn’t matter whether there is “2”, “4”or anything else on last position.

  11. Mar 22, 2008

    apparently i was mistaken, and my solution for 28year long period is not valid (this also applies for majek solution).

    reason is in manual to crontab:

    Note: The day of a command’s execution can be specified by two fields β€” day of month, and day of week. If both fields are restricted (i.e., aren’t *), the command will be run when either field matches the current time. For example,

    “30 4 1,15 * 5” would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.

    so, actually cezio is right – the longest period would be on 0 0 29 2 *. πŸ™‚

Leave a comment