Can a cron job kick off another cron job?

dmoneyman

New Member
Messages
19
Reaction score
0
Points
0
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?
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
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?
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
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
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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.
 

dmoneyman

New Member
Messages
19
Reaction score
0
Points
0
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?
 

dmoneyman

New Member
Messages
19
Reaction score
0
Points
0
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.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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:
[FONT="Courier New"]# 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[/FONT]

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.
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
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?
 
Top