Cron invoked function looping till given time - php

I am writing a task which will be ran by as a Cron Job. I want the function to check if a file has been downloaded to the server yet. The time stamp check is fine, however what is the best way to make the check loop run every 15 minutes? Could I use sleep and then check the current time stamp againsed the time stamp I want the loop to terminate (5am)?
Cheers.

This is what cron does. Crontab allows you to set an interval to run a script. It is easy to specify 15 minute intervals. This way you won't have to worry about sleeping or error recovery or anything in your php script, just your logic.
http://www.electrictoolbox.com/run-cron-command-every-15-minutes/

Related

Can we schedule cron job in the php script?

I'm new to this cronjobs and I want few emails (2k to 3k mails to be more precise) to be sent at specific time and date which are in the database table.Currently to achieve this, I'm calling my mail function file(sendmail.php) for every minute using cron job and comparing the current time and the time which comes from the db table, if true the mail will be sent.By doing this I'm afraid there will be some effect on the performance.
Can we schedule cronjob right after the insert query in the php script. So that I can pass those time and date variables to it?
Does calling the file for every minute in the cron job is a good practice? Will the performance get effected because my application will be used by 25 users at a time?
Although by calling the file for every minute achieves my task, but still want to know if there are any better ways.
Thank you in advance.
for every minute using cron
If you're firing off cron jobs every minute then you're doing some thing wrong. There are problems with jitter, and concurrency.
comparing the current time and the time which comes from the db table
Does that mean you are doing the tie check outside of the DBMS? That would be very silly.
Can we schedule cronjob right after the insert query in the php script
Yes, although you'd need to use sudo to create privilege separation. However you having (potentially) thousands of cron jobs is a very bad idea.
While there is a lot missing from your problem statement, based on what you have said, I'd suggest having a cron job running once every (say) 15 minutes, polling the database for the emails to be sent in that time window - with the time comparison and concurrency locking done in the database.

How to add dynamic cron job for php

I want to run php script at the particular time and then update the time to next interval. Is it possible to set cron job time duration from php script ?
For Example:
1 - set cron to run at the next sunday,
2 - then update the cron time to another particular time from php,
3 - so on..
You can set your cron job to run at a constant interval (every minute, every hour, etc.), and within the job, check if the action needs to be performed. Laravel framework, for example, does this with their scheduler. There is a single entry into the crontab, and within the job itself, it determines what it actually needs to run.
To keep track of the job internally, you can use a database to store the next run time. If the current time is greater than the next run time, you perform the action and update the database based on your rules for determining the next interval.

Create random minute cron job for a specific date and hour

I have been trying to do random cron jobs where I choose the year month date and hour but the minute is randomised.
My first attempt was to run the cron every min and then compare a random date with todays date:
I inserted a random date into a database column fake_time in the format 2014-10-26 17 rand(0,59). In the php page where I run the cron every min:
if($row["fake_time"] == date("Y-m-d H i")){
//do stuff
}
And this worked perfectly. But then I found out that I can't run the cron every min because my hostor (hostgator) wont allow me to! Have you got any ideas on how I can do this any other way?
Or should i just set it up on https://www.easycron.com/ instead?
I think you are being limited by the number of cron jobs you can run in a day, IIRC hostgator has a daily limit for basic plan. To work around this limitation, IMO, you have two choices:
Go to sleep for 60 seconds
Basically, run the cron job at the required hour every day, and check for your condition, if it is not True, then go to sleep for 60 seconds.
if($row["fake_time"] == date("Y-m-d H i")){
//do stuff
} else {
sleep(60);
}
This way, you have a single cron job, though it runs for a long while. In case you can run the cron job only daily, and you want to run at random hour as well as minute, you can change your logic and go to sleep for 3600 seconds for hourly sleeps, and then go for minutely sleeps of 60 seconds.
You might need to setup set_time_limit accordingly.
Set up easycron
In case your cron jobs are terminated abruptly because the time limit can't be set, you will need to hit using easycron service.
In this case, put the above script code in a php file, say script.php, and schedule a cronjob to hit with a get request on this script. Your command in this case will look something like
wget your.domain.com/script.php
If there are no technical problems, you can do it with your php script (no install required).
if(rand(1, 5) == 1){
// do your staff
}
If you think that the script will not be executed often enough, you can reduce the difference rand(1,3)

php time_sleep_until() and "Mysql server has gone away"

i have script which must execute after every n minutes. n minutes is dynamic so that i could not set a cron job to call the script (at a specific time).
so what i did was i stored the time after every n minutes in an array so that when the script is executed, it will first check whether the current time is in the array. if it is found in the array, it continues to executes otherwise it exits.
to execute the script, i must use a cron job to run every minute to check the time in the array. unfortunately, my web host only allows 5 minutes as the least interval. so every time the script is called, i check whether the values between $current_time and $current_time + (4*60) // 4 minutes is found in the array. if it is, and if needed, i use time_sleep_until to delay the script until the time reaches the value found in the array.
so if my script executes at 10:05 and the value found in the array is 10:06, i let the script sleep until 10:06 before it continues to execute. however, if the sleep time is more than a minute or so, i get a Mysql server gone away.
how can i prevent this? or is there a better way to do this?
thanks!
A couple choices, which is better I do not know.
One, is make sure your script works with CLI and after that minute is up call it with the http://www.php.net/exec function (if your host allows it).
Two, is setup a script, with a possible hash as a key and use a header redirect after the minute is up, this would call the script brand new so a new MySQL connection is made.
A third option is to set the script up like in two, except setup a schedule task / cron job on your computer that opens that page (it would have to be in the webroot) and calls it every minute or however you want. This is not a set method, but depends on how much your computer is on.
Fourth, similar to the third but use a free cron job hosting service like: http://www.onlinecronjobs.com/en
Hope that helps. If I think of other options I will update.

To use sleep() or cron job

I have this mail script I have to run a few times.
To start the script I will use cron, but the script has to run 2 or 3 more times (with an hour apart).
What's the best way to do this?
To use the sleep command for an hour, or at the end of the script, place some code, so that the script will create a new cron job to run it self after an hour?
Thanks
Unless there's some cost savings in keeping the script running in memory, you're better off using cron to invoke it every hour, as needed.
0 0-2 * * * /usr/local/bin/mail-script.php
You can choose multiple hours using the - syntax, or the comma syntax:
0 0,1,2,3 * * * /usr/local/bin/mail-script.php
If it needs to maintain some form of state, use a temporary file to keep saved state.
Do:
> man 5 crontab
To see if your *nix handles the above cases.
Finally, unless you know the script has to run only 2-3 times, you're better off putting the logic about whether to "run or not to run" in the PHP script itself, and then just run it every hour.
One advantage of using sleep() is that it could be more portable. For example, on many systems I work with, users are not allowed to have their own cron jobs - so writing your program to take care of its own timer-ness might be an advantage.
An alternative to sleep() might be using SIGALRM (so your script catches an interrupt and executes code at a certain interval - when that interrupt is thrown.)
I mean, I'd recommend using cron - but here are some alternatives!
I'm not sure either approach (sleeping for an hour, or creating cron jobs from php) is ideal, how about a cron job that runs every hour anyway, then your php script checks whether it should run?
Why not just set the cron criteria so it fires those specific times? Cron is pretty flexible in that aspect.
Update your question with when you want it to fire off and I can give an example.

Categories