+ Reply to Thread
Results 1 to 9 of 9

Thread: Can a cron job kick off another cron job?

  1. #1
    dmoneyman is offline x10Hosting Member dmoneyman is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    19

    Can a cron job kick off another cron job?

    Is it possible to have 1 cron job kick off one of 2 other cron jobs for the next day rather than both on a set time?

  2. #2
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Can a cron job kick off another cron job?

    Do you mean that you want a cron job to schedule another cron job?

    If that is your question, why not have one script that behaves differently depending on an external variable?
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  3. #3
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Can a cron job kick off another cron job?

    It sounds like you want a different job to run each day, so why don't you have 3 cron jobs and tell them to check what day it is? Maybe you can do something like:
    Code:
    pseudocode
    // run on odd days
    if day_of_year % 2 == 1
    then execute
    // run on even days
    if day_of_year % 2 == 0
    then execute
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Can a cron job kick off another cron job?

    If you want a job to run sporadically, you can use at (the exact syntax of at may depend on the OS flavor, so here's another manpage). If you want it to run periodically, use one of the above solutions.

  5. #5
    dmoneyman is offline x10Hosting Member dmoneyman is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    19

    Re: Can a cron job kick off another cron job?

    Quote Originally Posted by xav0989 View Post
    Do you mean that you want a cron job to schedule another cron job?

    If that is your question, why not have one script that behaves differently depending on an external variable?
    Right now that's what I'm doing in a way. But I really think it would be better in my situation for a cron job to schedule another cron job. Is it possible?

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Can a cron job kick off another cron job?

    Quote Originally Posted by dmoneyman View Post
    Right now that's what I'm doing in a way. But I really think it would be better in my situation for a cron job to schedule another cron job. Is it possible?
    What is your situation?

  7. #7
    dmoneyman is offline x10Hosting Member dmoneyman is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    19

    Re: Can a cron job kick off another cron job?

    I have one cron job that does some work on a 1x daily basis.

    Approximately once a week, there's updates in needs to run every 10 minutes instead of every 1 day.

    I don't want to have a cron job that runs every 10 minutes every day. If there was a way that the daily cron job can kick off the 10 minute cron job on an as-needed basis, that would be ideal.

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Can a cron job kick off another cron job?

    Quote Originally Posted by dmoneyman View Post
    Approximately once a week, there's updates in needs to run every 10 minutes instead of every 1 day.
    Is the day these updates need to run dependent on an external condition or is it a predetermined (albeit semiperiodic) time? What's the condition for stopping the scheduled jobs?

    If you need to run a few of the 10 minute updates, at might be the simplest way to go.

    If you know ahead of time when the updates will run (that is, the time isn't dependent on an external factor), you could write a more complex schedule for the updates cron entry. You might need to write multiple entries. For example,
    Code:
    # Run updates every 10 minutes on even hours on the 4th and 18th, 
    # and on odd hours in the morning and evening on the 12th and 26th
    */10	*/2		4,18	*	* updates
    */10	1-7/2,21-23/2	12,26	*	* updates
    I believe all modern crons (according to the manpage, absolut runs ISC Cron V4.1; ISC is the newer name for Vixie cron) support lists of ranges and range steps, but I could be wrong.

    If that won't work, you can:
    1. use crontab -l to get your current crontab,
    2. add (or uncomment & alter) a line for the updates,
    3. save that to a file $crontab,
    4. then run crontab $crontab to install the crontab with the update script scheduled.
    To disable the update script, do basically the same thing but comment out the line for the scheduled update script in step 2. This approach is error prone, which is why a complex schedule is better than updating crontab. If you must use the last approach, add numerous sanity checks: when you schedule the update script, run it only on the current day and have the update script double check that it should run.

  9. #9
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Can a cron job kick off another cron job?

    It sounds to me like maybe you're taking an approach to this that is more difficult than necessary. Maybe you can code your webpage to do the work as necessary.

    For example, on my site I need to make sure a directory contains all the images needed to generate some content on the page. If one of the images is bad or missing, I have a script that will re-make them (they're very small images and there's about 70 of them) when a user loads the page. This adds maybe 1 second to the user's load time. So, maybe you can do something like this?
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

+ Reply to Thread

Similar Threads

  1. CRON Jobs and PHP
    By deadimp in forum Tutorials
    Replies: 14
    Last Post: 11-27-2008, 05:09 PM
  2. Cron Jobs
    By HackerzInc in forum Free Hosting
    Replies: 1
    Last Post: 10-01-2008, 10:06 PM
  3. Cron again again and again
    By Mika56 in forum Free Hosting
    Replies: 0
    Last Post: 08-18-2008, 12:03 PM
  4. Cron Tutorial (Crontab Tutorial)
    By sunils in forum Tutorials
    Replies: 3
    Last Post: 06-14-2008, 10:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers