Jump to content

Cron Job For Every Day of the Week


Byron

Recommended Posts

Here's a way to have 7 seperate cron jobs but only use one of your allowed two cron jobs. I think I posted this a long time ago but since somebody recently posted about how many cron jobs they were allowed, I thought I would post it again. This simple script will let you run a cron job for every day of the week. Create a php file and name it something like "curl-cron.php" and paste the code below into the file:

 

<?php
$today = date("w");
$array = array( 
   "http://url-sun.php",
   "http://url-mon.php",
   "http://url-tue.php",
   "http://url-wed.php",
   "http://url-thu.php",
   "http://url-fri.php",
   "http://url-sat.php"
  );

$url = $array[$today];

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
curl_close($ch);
?>

 

Then go to your cron manager at your cpanel and create a cron job to run once every 24 hours. Something like this:

 

php -q /home/byron/public_html/curl-cron.php >/dev/null 2>&1

 

Then edit each day of the week inside the curl script above with a cron job. I use it to empty out old files in several directories on my site.

 

 

Link to comment
Share on other sites

A cron job is a scheduled task that is run every so often (specific to the webmaster). Every time a cron job is run, it executes a command onto the server. HelioHost has a limit of 2 cron runs (not jobs, runs) per day. ;)

 

For example, you could make a cron job that clears a MySQL Database every 12 hours. However, with byron's script you can do much more advanced scripts :)

Link to comment
Share on other sites

  • 2 months later...

My cron script is legal the way it's set up. We are allowed 2 cron runs a day, but we can have 50 cron jobs if we want, just as long as only 2 out of that 50 get run in a 24 hour period. So with my script, I'm just using one of my allowed two cron runs and using it to run 7 tasks, each one on a different day of the week. I clean out several folders on my site and a few other things via cron. You could actully go to the cron manager and set it up that way, but it would be more of a pain to figure out the days and different times. I've even written one that will run a cron task for each day of the month, but I don't have enough tasks to use it.

Link to comment
Share on other sites

My cron script is legal the way it's set up. We are allowed 2 cron runs a day, but we can have 50 cron jobs if we want, just as long as only 2 out of that 50 get run in a 24 hour period. So with my script, I'm just using one of my allowed two cron runs and using it to run 7 tasks, each one on a different day of the week. I clean out several folders on my site and a few other things via cron. You could actully go to the cron manager and set it up that way, but it would be more of a pain to figure out the days and different times. I've even written one that will run a cron task for each day of the month, but I don't have enough tasks to use it.

 

Byron, please don't take offense to what I said. Yes your script is set up legally, but I think users here will abuse this.

Essentially, you're suggesting to set up your own scheduler because of the limit that is imposed. Imagine if I did this:

  1. run a script that runs every minute of every day
  2. the script parses what time it is and runs certain programs based on what time it is

Sure I'd be running one script, which follows heliohosts rules, but it calls multiple other scripts on a schedule, circumventing the rules that cron is trying to restrict. In programming terms, this is called a "wrapper." I think the sole reason of imposing the restrictions is to reduce the impact the scripts are imposing on the server, so if we created our own scheduler, we would be getting around this limitation and hurting the server.

 

As for your post, I was saying that instead of having different scripts for each day of the week, you could just run one script at the same time every day, which runs different functions based on what day/time it is.

Link to comment
Share on other sites

Imagine if I did this:

  1. run a script that runs every minute of every day
  2. the script parses what time it is and runs certain programs based on what time it is

If you did that and you were using cron to run your script every minute of the day, I would suspend your account. If you did it by some outside cron service then we don't have any problems with that.

I was saying that instead of having different scripts for each day of the week, you could just run one script at the same time every day, which runs different functions based on what day/time it is.

It's all the same whether I have multible functions inside one script or I divide it up into seven scripts. Only one script is getting run once every 24 hours.

 

Like I said ealier, I could actually go to the cron manager and do exactly what I'm doing now by setting up several cron tasks, each task with a different script. Anyway, we (the administrators) keep an eye on cron jobs and nobody goes un-noticed. :)

Link to comment
Share on other sites

  • 3 weeks later...

If you did that and you were using cron to run your script every minute of the day, I would suspend your account. If you did it by some outside cron service then we don't have any problems with that.

 

Does anyone have a recommended outside cron service? I wish to implement a cron-job which kicks users who have been inactive for over 1 hour. I would probably run it every hour.

(A tiny task compared with other things possible with a cronjob, but as with all things, rules exist because some one at some point has done something incredibly stupid).

 

I've never considered to possibility of an external service before- any recommendations?

Link to comment
Share on other sites

Just did the math on their premium system. 1 cron job / hour for one year = $90.

 

Thinking about it some more, I have a better idea for the time being.. There's actually nothing stopping me from writing a page which makes ajax requests according to an array of timers and reads/updates a mysql_table on timer expiry.

 

Of course that means the jobs are dependent upon my connection. Nothing i'm doing here is life or death, just basic maintenance, so it shouldn't matter.

Link to comment
Share on other sites

I've misread their system it seems. I thought resource points were taken up per execution, not as a fixed value for the task. I'll give it a go. That means theoretically that you could have 12 hourly concurrent runs without a log, for free and still have enough pts left for a logged daily run.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...