SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: Scheduled tasks

  1. #1
    SitePoint Enthusiast Zero G's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Scheduled tasks

    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

  2. #2
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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 BlogTwitterContact me
    Neon Javascript FrameworkJokesAndroid stuff

  3. #3
    SitePoint Enthusiast Zero G's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...

  4. #4
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:
    Code:
    SELECT sql_command FROM [table] WHERE last_execution + interval < UNIX_TIMESTAMP(NOW())
    Then you would simply execute all the queries and update the last_execution column in the database.

    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

  5. #5
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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 BlogTwitterContact me
    Neon Javascript FrameworkJokesAndroid stuff

  6. #6
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  7. #7
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 ??

  8. #8
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  9. #9
    SitePoint Enthusiast Zero G's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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">

  10. #10
    SitePoint Enthusiast lacerus's Avatar
    Join Date
    Aug 2003
    Location
    Hamburg
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...

  11. #11
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you only wanted to run the task when a script was run, then you wouldn't need scheduled tasks in the first place.
    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.

  12. #12
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by HarryF
    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.
    That looks cool.

    --ed


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •