Hi,
I was wondering how to use PHP to set a cron to run a PHP script at given intervals. I would need a way to do this on both windows and Linux systems.
thanks![]()
| SitePoint Sponsor |
Hi,
I was wondering how to use PHP to set a cron to run a PHP script at given intervals. I would need a way to do this on both windows and Linux systems.
thanks![]()
It is not possible to set a scheduled task using PHP in a way that will work on all systems. This is not something that PHP can do - you would always be relying on the operating system's own scheduled task system, which is different depending on the operating system (and in many cases you wouldn't have access to from PHP).
[mmj] My magic jigsaw
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The Bit Depth Blog · Twitter · Contact me
Neon Javascript Framework Jokes
I don’t mind if the code to do this is different for each OS. Would it be possible to set this using a Windows API call for a windows comp. and is there a way to do it on LINUX?
Anyway... if not do you have any other suggestions on how to at least simulate scheduled tasks?
I really need to run an hourly script that handles some cleanup and whatnot...
![]()





workaround... yes it exists (as always)
You could just save some "jobs" in the database with the last time it was executed and how frequently it is supposed to be executed. Say for example that you only need to execute sql commands, then you could have table with four columns, id, sql_command, last_execution and interval.
Then you would store the times in eg unix timestamp.
On each hit you could execute the following sql command:Then you would simply execute all the queries and update the last_execution column in the database.Code:SELECT sql_command FROM [table] WHERE last_execution + interval < UNIX_TIMESTAMP(NOW())
If you need to do something other then just sql commands you could either store plain php code in the database and then use eval or store commands in some specific syntax in the database and then parse it to know what that job is supposed to do.
Anyway, long enough...
Hope this helps.
- website
The problem with the workaround that website posted is that it is not capable of emulating a scheduled task, because it can only run when you run a script. If you only wanted to run the task when a script was run, then you wouldn't need scheduled tasks in the first place.
So, this workaround is useful in many circumstances, but not when you really need scheduled tasks.
[mmj] My magic jigsaw
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The Bit Depth Blog · Twitter · Contact me
Neon Javascript Framework Jokes





well, yes, but he says it is for cleaning up (some files/database I expect). I suppose he would like the web site to clean itself up... ...so that cleaning will have no affect on anyone unless he visits the page. If he does then this cleaning is done when the user visists the page and the result will be the same.. (well, okay, the cleaning queries would be executed when the user asks for the page which could slow it down once in a while but for portability I would still use that method...)
- website





There is a cleaner solution that this, was suggested a while back, cannot remember the link though.
Do a determined (eh ?) search for CRON maybe help ??





I do know what CRON is, cron is a very useful tool indeed but if portability is what you need you cannot use it. I don't know of a way to modify the windows schedule tasks via php for example.
Modifying the crontab in linux is rather simple from php but the user "apache" or "www" or whatever name it is must have access to it which is not something I would except..
- website
You made a good point website; users might not have access to it, which is something I didn’t even think about.
Anyway I'll probably end up doing it the way website said. But would it be better to call the file to do the task with an image tag? For example
<img src="hclean.php">
It does not matter whether you do it like that or include('cron-workaround.php') in your header/prepend/whatever file. The latter is probably faster, as I can imagine that it produces less HTTP overhead, but this is really an uneducated guess...

Yes and no. You can use general traffic to your site as the "ticker" for your batch job processor. This is the approach used by pseudocron: http://www.bitfolge.de/pseudocron-en.html - the normal way to use it is to "include" pseudocron as an image within your HTML. Works pretty well - more reliably on sites with higher traffic of course.If you only wanted to run the task when a script was run, then you wouldn't need scheduled tasks in the first place.





That looks cool.Originally Posted by HarryF
--ed
Bookmarks