SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 75
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php daemon vs Cron Job

    Dear All,
    I have an application where I have to time to time keep checking the db if there is any update I got to run some function. So what is recommended cron job or php daemon.

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    I would recommend a cron job. Writing a daemon in PHP is more complicated, while cron will do exactly what you need.

  3. #3
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Immerse,
    I read for cron there is one problem of overallping. Say my cron start at minute one and stretch over minute 2 then at minute 2 cron starts another instance of it and if this goes one the server will go down any solution for this?

  4. #4
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Increase the interval i would say.

    Writing a daemon is ok in theory but then you'd need to be sure you can run it and on a shared server this is often not allowed.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    For the overlap you can also use a lock file. Take a file name, e.g. "cron.lock" and check if it exists when the cron starts. If it does don't continue. If it doesn't create one and delete it when you're done.
    That, plus you know that cron jobs won't start at the exact moment (which could still give issues with the lock file) guarantees you'll never have more than one running at the same time.
    The only annoyance here is that if a cron job created the file and then crashed the file will still exist and the cron job will never run anymore until you delete that file.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Scallio,
    How do we create the file via shell script is it? Any codes for that ? Secondly in the event of crash how to go about it?

  7. #7
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Tangoforce,
    Server is not an issue we have our own dedicated ones. If you say increase the interval referring to cron or php daemon? U have any link how to write basic phpdaemon?

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You could do something like this:

    PHP Code:
    $lockfile='./cron.lock';
    $timeout=30 60// 30 minutes
    if (file_exists($lockfile))
    {
      
    $fp=fopen($lockfile'r');
      
    $ts=fgets($fp4096);
      
    fclose(fp);
      if (
    $ts time() + $timeout)
        exit();
    }
    $fp=fopen($lockfile'w+');
    fputs($fptime());
    fclose($fp);

    // rest of cron code

    @unlink($lockfile); 
    When the cron starts it will write the current timestamp to the file. If another cron job starts it will open that file and read the timestamp. If it's less than 30 minutes ago the cron will not run. If it was written 30 minutes or more ago it will run (under the assumption that some other process crashed earlier without deleting the file).
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Scallio,
    Thank you for the code snippet. Give me some time I need to see how to run this file and adapt it. So I save this file as .php is it? Then I call it via shell script to be a cron is it?

  10. #10
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Yes and yes

    Of course you need to put your own code in the spot where I put // rest of cron code
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  11. #11
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Scallio,
    I know where to put my codes ready thank you. I will try if not I will get back to you. Another thing say I save this file as service1. php so under crontab -e can I put like this */1 * * * * /usr/local/service/service1.php. Then restart the cron service?

  12. #12
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    No, you can't execute php files by themselves. You need to call the php exectutable with the php script as a parameter

    Code:
    */1 * * * * /path/to/php /usr/local/service/service1.php
    you need to put the full path there, just "php" might not work because cron jobs aren't run in the same environment you are in as user.

    If you don't know where the php executable is, type

    Code:
    whereis php
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  13. #13
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Scallio,
    Below is the results of whereis php. So my guess will be /usr/bin/php is it?
    php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/share/php /usr/share/man/man1/php.1.gz

  14. #14
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Yup
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  15. #15
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Scallio,
    Give me some time now to play around as I am very new to all this. Thank you once again.

  16. #16
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Dear Tangoforce,
    Server is not an issue we have our own dedicated ones. If you say increase the interval referring to cron or php daemon? U have any link how to write basic phpdaemon?
    I meant to increase the delay in the cron however Scallio has also put forward a pretty good idea.

    With regards to a daemon, see my comments below.

    Quote Originally Posted by ScallioXTX View Post
    No, you can't execute php files by themselves.
    Indirectly you can using a loop, set_time_limit(0) and ignore_user_abort(true). Call the page, click the stop button and the script will continue looping until it say finds a 'stop' file or a marker in a database. Not quite the same but it would be easier than creating a daemon to run as its own process. I know i've used this technique for several things and it works well on both windows and *nix systems. I'll admit its a bit crude and possibly frowned upon by the programming elite but it does work and its simple to put into action.

    Sample here (untested):
    PHP Code:
    //No time limit
    set_time_limit(0);

    //Ignore the browser closing connection
    ignore_user_abort(true);

    while(!
    file_exists('stopfile.txt'))
       {
       
    //Do something

       //Anti CPU locking
       
    sleep(1);
       } 
    I know its crude but it does the job..
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  17. #17
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Tangoforce,
    When you say a daemon here is to create php file and let it run under linux cron am I right just to make sure I am using the right terminology. I dont quite get your method when you use the stopFile.txt who creates the file and what to store in that file? Thank you.

  18. #18
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A daemon is a process / program that runs by itself. Like a windows .exe program. I got this from dictionary.com:
    daemon definition
    operating system
    /day'mn/ or /dee'mn/ (From the mythological meaning, later rationalised as the acronym "Disk And Execution MONitor") A program that is not invoked explicitly, but lies dormant waiting for some condition(s) to occur. The idea is that the perpetrator of the condition need not be aware that a daemon is lurking (though often a program will commit an action only because it knows that it will implicitly invoke a daemon).
    You started this topic by talking of creating a daemon. I think most of us assumed you might write a program in c/++ or something similar that would run on your server.

    All I am saying is that you can do that, run a cron script OR run a php script in a loop as i have demonstrated which would run in a similar method to both of the above. The idea is that the script would loop continuously (sleeping for 1 second to avoid locking the cpu) until you tell it to stop - eg by placing a small text file on the server. You don't even need anything in the file just a blank file with the correct filename would do it.

    I'm just trying to give you one more option to consider.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  19. #19
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Tangoforce,
    Thank you for the indepth explain have given me more clarifications. What I am a bit confuse here is that when you php script in a loop ? How do you execute that script? Can brief me on that ? Thank you.

  20. #20
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You call it via the browser like any other php script. You won't see any html output but you can then just hit the stop button it will continue looping.

    You could theoretically do this to confirm its running but its not guranteed:
    PHP Code:
    print 'Running';
    flush(); 
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  21. #21
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Tangoforce,
    If I do not want to involve the browser is that possible. Let me explain my scenario. In the backend I am keep receving data into my database. So what I would like to check if the particular data was in particular geo fence and also capture the alert via the data stored in the db. So basically this script is to process the data. So what is your best approach.

  22. #22
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you don't want to involve a call via the browser then your best bet is cron and cron only. Scallio has already given you an excellent method to use that.
    Last edited by tangoforce; May 17, 2011 at 03:33. Reason: Typo IF => If
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  23. #23
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Tangoforce,
    I know why I dont want the browser is that in the event if some one stop the browser or close etc. So what do you think about my problem where I am processing the data. Is this method suitable or some other method better. So on every minute I will check new data and process it.

  24. #24
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did explain above that it stopping or closing the browser doesn't matter. You make one call to the script via the browser and then close it - the loop continues to run with no browser.

    If you look at my code you will see two functions:
    set_time_limit(0): Allows the code to run forever

    ignore_user_abot(true): Means that even when the stop button is pressed, or the browser is closed the code will continue to run.

    This means that the code i gave you above will loop every second.. with no browser.. until you put a file called stopfile.txt in the very same directory.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  25. #25
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Tangoforce,
    Sorry now I can see clearer with your recent explantion.How to know if the script is running or not? Another thing is that in the event if there is any error generated using your method how to log it?


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
  •