Php daemon vs Cron Job

Discussion in 'Programming/Scripts' started by newbie14, May 15, 2011.

  1. newbie14

    newbie14 New Member

    Dear All,
    I have an application where from time to time I need to check the db for some value and run some functions to do some checks. What is the best option do it as php daemon or cron job?What I worried of cron job is the overalapping.
     
  2. Ben

    Ben HowtoForge Supporter

    My first question would be, how you implement that kind of deamon in code.

    If you do not have the need to directly call the deamon to run and it's ok for you to have the job run on a static schedule, a cronjob should be fine for you. The mentioned issue of overlapping can be cought when using a kind of lock-file or similar to check whether your script is already / still running. Based on that you can decide whether you end this probably overlapping job run or if you just kill the old process....
     
  3. newbie14

    newbie14 New Member

    Dear Ben,
    How to exactly overcome the overallping problem. I am quite clear on it. Thank you.
     
  4. till

    till Super Moderator

    Better use a cronjob. A daemon written in php tends to accumulate more and more memory until it fails after a few days. PHP is not a good language to write deamons.

    Regarding the overlapping cron job, create a lockfile when your php code starts and there is no lockfile. This file gets deleted when your code is finished. If your script is run and finds already a lockfile, it quits directly.
     
  5. newbie14

    newbie14 New Member

    Dear Till,
    I dont quite get the idea of lock file. How do I go about I am very new to it? Can you give me more description?
     
  6. till

    till Super Moderator

    Here some php code as explanation (untested):


    <?php

    // Clear the statecache
    clearstatecache();

    // we will stop here when a lockfile exists
    if(is_file('/tmp/mylockfile')) die('There is already a instance of this script running');

    // Set the lockfile
    touch('/tmp/mylockfile');

    // Here we do some work that may take a few minutes
    sleep(600);

    // We are finsihed, so we remove the lockfile
    unlink('/tmp/mylockfile');

    ?>


    When you execute this script a second time while the first one is still running, it will not execute the code (here the sleeep command) again. Instead it exits with the message "There is already a instance of this script running".
     
  7. newbie14

    newbie14 New Member

    Dear Till,
    So I should save the above code as .php then I run it via the cron tab settings is it?
     
  8. till

    till Super Moderator

    The baove code is an example for a file based locking mechanism. You can run it on the shell or as cronjob, thats up to you. But running that script as cron makes not much sense as it does nothing else as waiting for 600 seconds. So if you want to test it, then do the tests on the shell and add you own code before you create a cronjob.
     
  9. newbie14

    newbie14 New Member

    Dear Till,
    When I run from shell here should be using the php executable right?
     
  10. eyeoncomputers

    eyeoncomputers New Member

    This may be a bit oldschool but...

    Code:
    while(1) {
    
    //some logic
    //sleep for some period of time
    
    }
    
    Then make the script executable and run it. You can run it in the background or even set it up to run at boot time.
     
  11. till

    till Super Moderator

    This wont work that well with php as php tends to consume more and more memory until it fails.
     
  12. newbie14

    newbie14 New Member

    Dear Till,
    To elaborate further actually I capture my data via a java listener then I insert into the db. Then I will pick those unprocess and process them via this php or cron job. Do you think I should process it in the java itself? Would it make the java programme to be burdened?
     
  13. till

    till Super Moderator

    May anser was only regarding the message from @eyeoncomputers and why it is better to use a cronjob and not run a daemon in php.

    If you process your data in java or php is up to your programming skills, use the language that fits better for you.
     
  14. newbie14

    newbie14 New Member

    Dear Till,
    I mean based on your experience will be better to handle those queries in java itself or do it separately after the insert into db via another php. What is your opinion?
     

Share This Page