ok, I know you can use cron to execute php scripts but can you use php to start these cron jobs in the first place?
PHP and Cron
You mean you want a cron job to run when a web page is accessed or something?
not every time the page is accessed but it would be good as an admin facility.
I'm trying to start a site that is a game of sorts. Players register and log in etc, they sign themselves up to a game. When there is enough players the game begins. The game lasts 48hours. Then I need certain things to happen.
Try to use a PHP "exec" to add a line to cron with crontab, check "man crontab" how to add from the commandline, should be something like crontab - -u username < '30 0 * /usr/.....'
Originally posted by jasper
Try to use a PHP "exec" to add a line to cron with crontab, check "man crontab" how to add from the commandline, should be something like crontab - -u username < '30 0 * /usr/.....'
Yeah, what he said. Depending on the the output you need back you can try: shell_exec, passthru, exec, or system. If you don't need any output back go with exec.
ok, I know you can use cron to execute php scripts but can you use php to start these cron jobs in the first place?
Well, if the cron is simply running a php script, then I assume what you mean by using php to start the cron, is using php to run another php script, which include() or require() will do.
Just do:
<?
include ("filename.php");
?>
When you run that, the file filename.php will be run.
Unless I'm missing something here, which is possible
A cron job is simply an automatic job that runs at certain times\days\dates etc... depending on how its setup.
Edit
I'm trying to start a site that is a game of sorts. Players register and log in etc, they sign themselves up to a game. When there is enough players the game begins. The game lasts 48hours. Then I need certain things to happen.
Okay, try this.
Add some code to your registration page, every time a player signs up successfully query the db for the number of players now signed up, if that number is equal to the required number of players for the game to begin, include the script that starts the game running (or, as it may be, just send an update query to the database that makes the needed changes to start the game).
Hope that helps!
Starting doesn't seem to be the problem, you can do that by checking if there are enough players every time a new player signs in. The problem was to end the game after 48 hours without user intervention.
Didn't realise that.
Okay, just add some quick code into a common file on each page where the player "does" something.
Top of the page, put it as an include of something, just a quick check of the time to see if times up (store starting time in db I guess). If times up, put up a message, end the game. If times not up, just let it continue on.
Its that or run a cron every minute, which isn't good for three reasons.
- Unless your on your own dedicated server your account will probably be terminated or you'll at least get nasty emails
- If a player hits a page while a cron is running sometimes (actually, really rarely, but anyway...) it gives a little error about something.
- Watch performance die miserably with a one min cron, no matter how small.