This discussion is archived
13 Replies Latest reply: Nov 15, 2012 6:41 AM by 800381 RSS

cronjob to run in every 40 days

Saurabh Gupta-OC Newbie
Currently Being Moderated
HI,

I want to schedule one cronjob to run once in every 40 days.

Please suggest what would be exact cronscript for this.

Thanks,
Saurabh
  • 1. Re: cronjob to run in every 40 days
    Dude! Guru
    Currently Being Moderated
    As far as I know you cannot do that. Similarly you cannot set an alarmclock to wake you up every 25 hours. You can only make settings within the constrains of valid calendar units. Recurring number of days per year is not a valid calendar unit.
  • 2. Re: cronjob to run in every 40 days
    Veeresh.S Pro
    Currently Being Moderated
    every 40days i think its not possible using cron............


    nearest you can think of running by-monthly or running on particular day for every 2 months........

    #If i am not wrong below cron schedule runs on 15th for every 2 months once

    * * 15 */2 */usr/bin/date >> /home/oracle/scriptlog

    #runs for every 2 months

    * * * */2 * /usr/bin/date >> /home/oracle/scriptlog
  • 3. Re: cronjob to run in every 40 days
    Saurabh Gupta-OC Newbie
    Currently Being Moderated
    Thanks for your reply.

    Can it be possible using AT (linux scheduler)?

    Regards
    saurabh
  • 4. Re: cronjob to run in every 40 days
    Veeresh.S Pro
    Currently Being Moderated
    877938 wrote:
    Thanks for your reply.

    Can it be possible using AT (linux scheduler)?

    Regards
    saurabh
    i dont think you can accomplish it even using AT.............

    however what are you trying to schedule.................if its something related to oracle and if you are open to oracle then you can think of using dbms_scheduler for that............a pretty impressive one........i went through oracle docs, scheduling a job for every 40 days seems possible :)

    Edited by: Veeresh.S on Nov 13, 2012 5:43 PM
  • 5. Re: cronjob to run in every 40 days
    Dude! Guru
    Currently Being Moderated
    There are alternative options. For instance, you could schedule a script to run daily and program it to check and increment a counter, saved and maintained in a file. This would also allow cron to pickup the correct remaining days for your schedule after a system restart. Or schedule crontasks using fixed dates instead of specifying an interval.
  • 6. Re: cronjob to run in every 40 days
    bobthesungeek76036 Pro
    Currently Being Moderated
    Why wouldn't this be possible with "at"? I admit it's not something I would do but it is possible:

    <pre>
    $ echo "ls -l /tmp > /var/tmp/tmp.lst 2>&1" | at now +40 days
    job 5 at 2012-12-23 10:33
    $ at -l
    5 2012-12-23 10:33 a user1
    $
    </pre>
  • 7. Re: cronjob to run in every 40 days
    Saurabh Gupta-OC Newbie
    Currently Being Moderated
    Thanks buddy.

    Your solution is excellent and working fine.


    For every problem there is a solution :)

    Regards
    Saurabh
  • 8. Re: cronjob to run in every 40 days
    Dude! Guru
    Currently Being Moderated
    Keep in mind though that the at command does NOT run your task every 40 days, which you asked for. The at command simply schedules a task to run once at a later time.
  • 9. Re: cronjob to run in every 40 days
    bobthesungeek76036 Pro
    Currently Being Moderated
    Dude is absolutely correct. To run every 40-days, you would have to write the job so that it issues the "at" command at the end of the job to run itself again in 40-days...
  • 10. Re: cronjob to run in every 40 days
    Dude! Guru
    Currently Being Moderated
    I have never tried it, but it may work to combine cron with at, e.g. to create a cron task that runs once a month and uses at to delay the execution for another 10 days.

    Edited by: Dude on Nov 14, 2012 9:10 AM
  • 11. Re: cronjob to run in every 40 days
    800381 Explorer
    Currently Being Moderated
    Dude wrote:
    I have never tried it, but it may work to combine cron with at, e.g. to create a cron task that runs once a month and uses at to delay the execution for another 10 days.

    Edited by: Dude on Nov 14, 2012 9:10 AM
    But that would be once a month, just 10 days later than the day of the month you have your cron job set to run.

    I think one way to do it so that the job automatically repeats would be for your "at" job to resubmit itself to run in 40 days.
  • 12. Re: cronjob to run in every 40 days
    Dude! Guru
    Currently Being Moderated
    You are right. One might as well schedule the task for the 10th of each month. That was quite silly, I wasn't thinking. ;-(

    Well, seems the best way might be to use at to submit a job that continues to re-submit itself again, as previously noted a couple of times (seeing it now). Or create 9 tasks in cron that run on a fixed date, which I wrote before. Think I'd rather use the later, which is more obvious than running something in the back or something one might forget about later.
  • 13. Re: cronjob to run in every 40 days
    800381 Explorer
    Currently Being Moderated
    Yeah, an "at" job sitting out there running itself every 40 days isn't going to be easy to keep track of.

    On Linux the "date" utility supports the "+%s" format, which is seconds since the epoch. Divide that by 86400 and you have days since the epoch. Something like this:
    #!/bin/bash
    now=`date "+%s"`
    today=$(( $now / 86400 ))
    rem=$(( $today % 40 ))
    Put that in a cron job, and if the remainder is whatever single value you pick, run your job. Else do nothing.

    That will run every 40 days.

    The "%s" format specification to the date utility is a non-POSIX extension, so it's not available on strictly POSIX-compliant systems. Though it's pretty trivial to write something in C that will emit the current time in seconds since epoch. Perl probably can be used, too.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points