How to run a process on the server

software development

#1

Okay. So I thought I would ask what someone might suggest I do about this.

I want to run a php process every 60 seconds from the server, that would update records in a database. This would be records pertaining to every user in the database. So, it could become alot of updating.

So, I wasn’t sure of the best way to do this.

  1. I’ve thought of using a cron job, but wasn’t sure of the performance hit on a shared server for something like this.(anyone know? i don’t)
  2. I’ve thought of scrapping the whole ‘run on the server’ bit, and just use AJAX calls on the webpage(i don’t really want to do this)
  3. Also, thought of doing a mix of both. If user logged out, update their info from the server. If user logged in, update their info from the AJAX call.

And then I ran out of ideas, so that’s why I’m here.

What are some ways I can run php files, from the server, that would be feasible? and a way that wouldn’t hog all the resources?


#2

I like your #3 the most. Running a cron job every minute is a lazy way and can lead to problems down the line…

If too many processes are running or using too many resources for your user, your processes will be automatically killed, which is bad. Unless p.php only takes a few seconds to run AT THE MAX don’t cron it every minute.

Another thing to note is that each time you call php5.cgi you have to reload the entire php interpreter. A better idea might be to make p.php run every 10 minutes, and then within p.php you can use sleep and usleep commands to effectively pause the script and act like cron.

This runs p.php every 10 minutes with 0 output, it is shared-server friendly by utilizing the nice command.

*/10 * * * * /usr/bin/nice -n 19 /dh/cgi-system/php5.cgi /home/user/p.php 1>&2 &>/dev/null Note that the time that your php script is sleeping using sleep or usleep DOES NOT count towards the max_execution time limit, so sleep and usleep are very useful.

So if this is p.php you can have cron run it once every 10 minutes, but the effect will still be the same as if it was run every minute. But instead of loading php5.cgi every minute it only has to load every 10 minutes.

[code]<?php

function update_db_records(){
#mysql calls whatever
}

for ($x=0;$x<10;$x ){
update_db_records
sleep 60
}

?>
[/code]
Something else you might want to look at that goes along with your #3 option would be when a user logs out execute this code.

$fp = fsockopen("www.site.com", 80, $errno, $errstr, 30); if (!$fp) echo "$errstr ($errno)\n"; else { fwrite($fp, "GET /cgi-bin/logout.php HTTP/1.0\r\nHost: www.site.com\r\n\r\n"); fclose($fp); } and when a user logs in execute this code.

$fp = fsockopen("www.site.com", 80, $errno, $errstr, 30); if (!$fp) echo "$errstr ($errno)\n"; else { fwrite($fp, "GET /cgi-bin/login.php HTTP/1.0\r\nHost: www.site.com\r\n\r\n"); fclose($fp); }
This just executes the login.php or logout.php script, just like cron except you can pass parameters like login.php?thisuser, and like ajax but without the javascript.

basically the goal is to keep the number of processes running to a minimum.

_____
[color=#00CC00] _ _| _ _ _ _|_ _ (_|_\|<(_||_)(_|(_| |(/_ | [/color]
:~