How to add dynamic cron job for php - 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.

Related

Running a PHP script or function at an exact point in the future

I'm currently working on a browser game with a PHP backend that needs to perform certain checks at specific, changing points in the future. Cron jobs don't really cut it for me as I need precision at the level of seconds. Here's some background information:
The game is multiplayer and turn-based
On creation of a game room the game creator can specify the maximum amount of time taken per action (30 seconds - 24 hours)
Once a player performs an action, they should only have the specified amount of time to perform the next, or the turn goes to the player next in line.
For obvious reasons I can't just keep track of time through Javascript, as this would be far too easy to manipulate. I also can't schedule a cron job every minute as it may be up to 30 seconds late.
What would be the most efficient way to tackle this problem? I can't imagine querying a database every second would be very server-friendly, but it is the direction I am currently leaning towards[1].
Any help or feedback would be much appreciated!
[1]:
A user makes a move
A PHP function is called that sets 'switchTurnTime' in the MySQL table's game row to 'TIMESTAMP'
A PHP script that is always running in the background queries the table for any games where the 'switchTurnTime' has passed, switches the turn and resets the time.
You can always use a queue or daemon. This only works if you have shell access to the server.
https://stackoverflow.com/a/858924/890975
Every time you need an action to occur at a specific time, add it to a queue with a delay. I've used beanstalkd with varying levels of success.
You have lots of options this way. Here's two examples with 6 second intervals:
Use a cron job every minute to add 10 jobs, each with a delay of 6 seconds
Write a simple PHP script that runs in the background (daemon) to adds an a new job to the queue every 6 seconds
I'm going with the following approach for now, since it seems to be the easiest to implement and test, as well as deploy on different kinds of servers/ hosting, while still acting reliably.
Set up a cron job to run a PHP script every minute.
Within that script, first do a query to find candidates that will have their endtime within this minute.
Start a while-loop, that runs until 59 seconds have passed.
Inside this loop, check the remianing time for each candidate.
If teh time limit has passed, do another query on that specific candidate to ensure the endtime hasn't changed.
If it has, re-add it to the candidates queue as nescessary. If not, act accordingly (in my case: switch the turn to the next player).
Hope this will help somebody in the future, cheers!

Cron invoked function looping till given time

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/

How to add Multiple cron job using php?

What i try to do >
1.User visit my PHP base Page
2.My page collect its data
3.Add cron job using PHP (Q1.How to add Cron job using PHP? )
(this type of cron job command currently i am using ""/usr/bin/wget myweb.org/some_directory/file.php?uid=2738 >/dev/null""
4.Cron job runs a above URL and do a task with user id which takes almost 1 hour + time (Q2. Is it possible to run job at the same time or after one minute if possible how)
5.If another user visit the page on the same time above procedure is also happen for them too (Q3.So this is possible)
Sounds to me that what you need is not a cron job but a background worker process. Cron jobs are jobs that are run at a specific automated time (such as 2:30 PM every day), not every time a user visits your page which could be whenever.
In order to instantiate background worker processes, I strongly recommend using Pheanstalk. Check out the readme to see how to instantiate a worker process, it's pretty easy.

PHP Update Entry Every Hour

I'm working on a site that generates a dynamic image based on data from another site. The problem is that loading the data from the other site ever time the image is accessed is slow. The Image displays the current stats of a "team" on a tournament website. I want to make a cron job and database that work together to update the info on a specific "team" every hour from when the team was last updated. For example, I could have the following db field:
ID, Name, Url, Wins, Losses, Xp, DateLastUpdated
So with my cron job, I want to update the entries every time the current date is an hour from the date last updated. How should I do this? Is there a specific way I should store the date and time? Should I even use a date and how often should I run the cron job?
If you have a unix system, place a file in /etc/cron.hourly/myjob containing something like:
#!/bin/bash
php /path/to/script.php
or via crontab -e or any interface
01 * * * * php /path/to/sync-script.php
should do the job. It will run sync-script every hour:01
You don't need to store the last time the image was updated; you can simply set your cron job to run hourly. If the cron job is running once per hour and it's the only thing creating the image, you know that the image is exactly 1 hour old at the time your script is invoked.

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.

Categories