Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 15th May 2011, 19:33
newbie14 newbie14 is offline
Member
 
Join Date: Jul 2010
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default Php daemon vs Cron Job

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.
Reply With Quote
Sponsored Links
  #2  
Old 16th May 2011, 08:49
Ben Ben is offline
Moderator
 
Join Date: Jul 2006
Posts: 1,029
Thanks: 7
Thanked 62 Times in 56 Posts
Default

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....
Reply With Quote
  #3  
Old 16th May 2011, 19:38
newbie14 newbie14 is offline
Member
 
Join Date: Jul 2010
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear Ben,
How to exactly overcome the overallping problem. I am quite clear on it. Thank you.
Reply With Quote
  #4  
Old 16th May 2011, 20:02
till till is online now
Super Moderator
 
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 35,702
Thanks: 819
Thanked 5,319 Times in 4,172 Posts
Default

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.
__________________
Till Brehm
--
Get ISPConfig support and the ISPConfig 3 manual from ispconfig.org.
Reply With Quote
  #5  
Old 16th May 2011, 20:28
newbie14 newbie14 is offline
Member
 
Join Date: Jul 2010
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?
Reply With Quote
  #6  
Old 16th May 2011, 23:25
till till is online now
Super Moderator
 
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 35,702
Thanks: 819
Thanked 5,319 Times in 4,172 Posts
Default

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".
__________________
Till Brehm
--
Get ISPConfig support and the ISPConfig 3 manual from ispconfig.org.
Reply With Quote
  #7  
Old 17th May 2011, 05:31
newbie14 newbie14 is offline
Member
 
Join Date: Jul 2010
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear Till,
So I should save the above code as .php then I run it via the cron tab settings is it?
Reply With Quote
  #8  
Old 17th May 2011, 09:18
till till is online now
Super Moderator
 
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 35,702
Thanks: 819
Thanked 5,319 Times in 4,172 Posts
Default

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.
__________________
Till Brehm
--
Get ISPConfig support and the ISPConfig 3 manual from ispconfig.org.
Reply With Quote
  #9  
Old 17th May 2011, 10:47
newbie14 newbie14 is offline
Member
 
Join Date: Jul 2010
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear Till,
When I run from shell here should be using the php executable right?
Reply With Quote
  #10  
Old 21st June 2011, 15:22
eyeoncomputers eyeoncomputers is offline
Junior Member
 
Join Date: May 2011
Posts: 17
Thanks: 0
Thanked 1 Time in 1 Post
 
Default

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.
__________________
Solaris ZFS How To
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cron errors guimnk General 1 5th April 2011 09:20
ISPConfig3 Debian Squeeze Crontab - Log petrichbg Installation/Configuration 5 22nd March 2011 12:25
Problem with services!! banzaiwebstudio.com Installation/Configuration 7 19th May 2010 21:13
Unable to install ISPConfig bdonecker Installation/Configuration 21 26th May 2009 08:20
Apache2 Freezes celtic Server Operation 31 28th May 2007 17:18


All times are GMT +2. The time now is 16:36.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.