Jump to content

Updating cron jobs with php, using cron to execute php script - help!


Recommended Posts

Thanks for taking a moment to help me.  I will forewarn you that I am completely new to cron jobs.

 

Here's the basic situation:  A user provides an email address, message, date, and time via a form on a site.  That data is stored in MySQL.  When that date and time is reached, a PHP script should execute that retrieves the necessary data and emails the user with their message.

 

From the bit I understand about crons (and that isn't much) - the best method to make this work is through the use of cron jobs.  When the user provides the form data, a php script needs to somehow set a new cron job to occur at the user provided time which then executes the emailing sending php script when that time has arrived. 

 

I am utterly confused about getting php to somehow update some sort of cron job task list (add the new cron job with the user provided time).

 

I would like this to be compatible with Godaddy shared servers which use linux.  They support cron but it seems to be through a web panel.  If you aren't sure if your solution would work with Godaddy - that's fine - I am just interested in seeing how this would work with offsite non-dedicated hosting.

I have GoDaddy and I have looked at this a hundred different ways, so let me give you multiple techniques.

1: You can set up a cron using GoDaddy's web panel and have it run as often as you would like, for instance every minute. Just give it a log file that it can check and clear, then the other script can assign jobs to this log. When the cron starts it will check the log for jobs and complete them.

2: You can make sleeper scripts, this was an interesting experiment, the scripts will only be allowed to sleep for so long before GoDaddy times them out, I believe 15 mins was the max. If it wakes prior it can keep going for about 4 hours then GoDaddy stops execution of the script. My times may be off I don't have my laptop with the final scripts on it in front of me. Now having the script eventually killed is a problem. But you can solve this two ways, every time a new job is added you can restart the sleeper. Not a good enough solution, you can have the script prior to the time when execution would be stopped call a URL on the server and that url will be a script that starts the sleepers brother. Yes I had to run the scripts as brothers, one checks and sleeps and prior to closing it calls the brother, who then takes over. This gets much more complex, but if you want it explained I can do that.

I highly recommend number 1. There are some other techniques too, but GoDaddy is pretty restrictive when it comes to crons, or even better daemons.

I have GoDaddy and I have looked at this a hundred different ways, so let me give you multiple techniques.

1: You can set up a cron using GoDaddy's web panel and have it run as often as you would like, for instance every minute. Just give it a log file that it can check and clear, then the other script can assign jobs to this log. When the cron starts it will check the log for jobs and complete them.

2: You can make sleeper scripts, this was an interesting experiment, the scripts will only be allowed to sleep for so long before GoDaddy times them out, I believe 15 mins was the max. If it wakes prior it can keep going for about 4 hours then GoDaddy stops execution of the script. My times may be off I don't have my laptop with the final scripts on it in front of me. Now having the script eventually killed is a problem. But you can solve this two ways, every time a new job is added you can restart the sleeper. Not a good enough solution, you can have the script prior to the time when execution would be stopped call a URL on the server and that url will be a script that starts the sleepers brother. Yes I had to run the scripts as brothers, one checks and sleeps and prior to closing it calls the brother, who then takes over. This gets much more complex, but if you want it explained I can do that.

I highly recommend number 1. There are some other techniques too, but GoDaddy is pretty restrictive when it comes to crons, or even better daemons.

 

Thanks for the input.  Regarding (1) - I don't see an obvious way to make cron jobs run every minute via GoDaddy.  I do see twice an hour and at any five minute interval within an hour (e.g. 1:05, 2:05, 3:05, etc).

 

Can you give me some more information on setting the cron job to run every minute?  If running a cron every minute is possible with GoDaddy (and doesn't kill the server) - I can easily just make the PHP script check to see if its time to send off a message.

I wouldn't run it every minute. A lot of hosts (and I'm surprised GoDaddy doesn't do this) limit the crons to every 10-15 minutes. This helps from users overloading their servers by running resource intensive scripts every single freakin' minute.

 

Now, I don't know how to setup a cron job via PHP - in fact I don't know how possible it is running it off a shared host.

 

From what it sounds like, running a cron every 10-15 minutes should be fine. When it's running, just have it look for a message time within the past 10-15 minutes, and then you could delete that row (or set a flag saying message sent)

I wouldn't run it every minute. A lot of hosts (and I'm surprised GoDaddy doesn't do this) limit the crons to every 10-15 minutes. This helps from users overloading their servers by running resource intensive scripts every single freakin' minute.

 

Now, I don't know how to setup a cron job via PHP - in fact I don't know how possible it is running it off a shared host.

 

From what it sounds like, running a cron every 10-15 minutes should be fine. When it's running, just have it look for a message time within the past 10-15 minutes, and then you could delete that row (or set a flag saying message sent)

 

That is what I was going to resort to.  The problem is that I would ideally want the user to receive the message at the exact minute requested (more or less).  Every 30 minutes (only option I actually see available from GoDaddy) is just not close enough to accomplish what I am setting out to do but it may be the only choice.  What I was looking for as a method between running a cron job every minute (I agree, seems extremely intensive) and running it only every 30 minutes is to run it only when necessary by putting together an php-updateable cron list.

Ok so you made me go look and you are right, it's been awhile since I was messing with this topic. They limit the amount that you can all the script. But if you remember you can execute a script for a long time on the server like I said if you use sleep. SO create a script that gets called every hour. Then have it start up and check for jobs, execute the jobs then go to sleep. keep waking every 60 seconds or so and check for new jobs. If jobs present do them, else sleep again. Do this until time is 59 minutes from start time, then exit. By that time the next script will start up. What do you think?

Depending on if your traffic size is large enough, you could have each page load simply run one or two emails - then whatever is left over, the cron would take care of.

 

Ok so you made me go look and you are right, it's been awhile since I was messing with this topic. They limit the amount that you can all the script. But if you remember you can execute a script for a long time on the server like I said if you use sleep. SO create a script that gets called every hour. Then have it start up and check for jobs, execute the jobs then go to sleep. keep waking every 60 seconds or so and check for new jobs. If jobs present do them, else sleep again. Do this until time is 59 minutes from start time, then exit. By that time the next script will start up. What do you think?

 

Thanks to both of you.  I'll take a look at the sleep idea - see if it will work. 

 

The site just won't get enough traffic to have it execute enough on each visitor for that to be enough.

 

Ha, if all else fails, I could always just have a dev machine reload the script every minute ;)  Time to get rid of GoDaddy for this...

Ehh, I would be careful running a script that long. Although the script might be "sleeping" it is still having to use resources, and can cause overload on a server - especially if you're doing it for 60 minutes, every hour (making it run 24/7)

 

ok If you are concerned make sure you exit the script before starting it again. I doubt you are going to grab many resources simply checking for jobs and sending emails. Never the less you can monitor system load and decide if you should continue the script with:

$load = sys_getloadavg();
$maxload=2;
if ($load[0] > $maxload) {
       //here you can write a string to your error log file that would state that the system's load is too high and the script exited.
       exit;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.