Php daemon vs Cron Job

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.

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.

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.

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.

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?


<?
$logging = true;

//No time limit
set_time_limit(0);

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

while(!file_exists('stopfile.txt'))
   {
   //Log something - This will see a new line in the logfile every second
   db_log(time());
   //Anti CPU locking
   sleep(1);
   } 

function db_log($str)
   {
   global$logging;         // Is logging enabled
   if($logging)
      {
      $str = date('Y-m-d H:i')."  ".$str."\\r\
";

      $fh = fopen('log.txt', 'a') or die("can't open file");
      fwrite($fh, $str);
      fclose($fh);
      }
   }
?>

Dear Tangoforce,
What will appear in the log file actually? IS just the time is it? Is the event of any error how to log that? Is there any tool to monitor it? Another thing if my server is remotely located how to activate this code?

With the code i have provided just the time but you can use the db_log() function to log anything you like.

Detect it just as you would in any other php code you have written and then just:


db_log($Error);

The log file, or any other function / tool you create. It’s a process that you can’t see and unlike windows you can’t use the task manager to see it running or close it. Therefore you will need to use some sort of output in the loop to see it is running - like the time logging.

Via the browser as i’ve suggested many times. If you’re asking how to install it then you do that via FTP like you would any website.

Dear Tangoforce,
Ok so based on your experience using this method have you use for heavy processing someting like how I anticipate? What is your clue will my problem will adapt well with this browser method?

I’m unaware of your heavy processing task. All i can say is that in theory this will run more frequently than a cron set to run every two minutes as long as your task takes less than that to complete.

Unfortunately i can’t answer everything, there are somethings you must decide yourself (eg by trying bits of code and seeing how well it works in your scenario).

Dear Tangoforce,
So once I have start your sample code will it go to sleep or keep running for ever and ever? I guess it will sleep for 1 second is it sleep(1)? In the event it crash or go down any tool to alert or restart it ?

It MUST sleep for 1 second. This prevents it from a race condition where it will loop as many times as possible in 1 second and locking the CPU causing the machine to freeze. By making it sleep every second it runs one loop per second and for the rest of that second the processor can do other things. It will continue to run like this forever until you stop it.

You could write another script to check the log file for the last time an entry was made… OR you could write a timestamp to a database and then have another process check that every second to see if there is a timestamp within the last 2 seconds. If there isn’t you have two choices:

  1. The script has crashed - So you restart it automatically using file_get_contents()
  2. Wait another few seconds and check again.

Ultimately the only tools and methods to control this are the ones that you create and write yourself. If you don’t feel confident enough to do this then i highly recommend you look at the cron method again as it will be much simpler.

Dear Tangoforce,
To be honest I am very new to all this method. So I know I got a lot of question to clear my doubts. Is not a matter of confidence but I am kind of confuse too. On the other hand I am also learning more new and useful knowledge. So just kind of lost need to analyse further how to implement all your valuable knowledge.

If you can’t follow what the deamon code does I strongly suggest you don’t use it but go for the cron job method instead (like tangoforce says in #32).

What is it you’re trying to achieve? Can you describe it in words?

Dear Scallio,
Let me explain my problem. I have some gps devices which keep pumping gps data into a table. What happens is that those data is raw meaning we dont know whether it is in a geo fence area, any alerts etc. So then I want this service to pickup those raw data and process and store the results in another table so that report generation will be fast and also can send alerts via sms,email etc once detected. So based on this scenarion what is best mechanism. Just to let you know the data inserts are heavy.

Hmm, thats a difficult one.

Running a script every second will keep the workload smaller and dealt with on a reguler basis. The downside to this is your inexperience and not quite knowing how to implement the method where many coders would become creative.

Running a cron is a lot easier for you as a programmer but it will on the other hand leave larger workloads to be processed and may take longer to process them.

For the time being, until you are more experienced with the principles of input and output and controlling a loop, may i suggest you use the cron job temporarily until you are able to successfully implement my method?

Dear Tangoforce,
What will be the downside with cron? Why do you say larger workload to be processed? I dont quite get it? You mean too many waiting is it?

IF you intend (as previously mentioned) running your cron every 2 minutes then the amount of data supplied by your GPS devices may of built up to a large level which could potentially slow things down on your system.

Cron is only triggered once a minute minimum as far as i’m aware. If you can find a way to make it trigger more frequently then this could be an ideal solution for you instead of my original suggestion.

Dear Tangoforce,
Yes you are right in a minute itself things will be pilling very quickly. So look like your way will the best solution I guess. So in your way when I keep running the select statement to find the unprocessed data it will keep giving me the new lines I guess? So maybe I was thinking lets give the cron job a try first then if things get worse then fall back to your mechnanism. Do you have any other mechanism to solve this problem?

Unfortunately there isn’t really any other mechanism that can do it. It’s either Cron, a linux program/daemon or a looping php script as i’ve shown you.

Just out of interest, how is the GPS data put into your database? - Is it via a http call?