NYCPHP Meetup

[nycphp-talk] Run PHP script as service (every 10 seconds)

Jon Gilkison jon.gilkison at gmail.com
Tue Dec 16 04:49:25 EST 2008


#!/usr/bin/php
<?php

/**
  * Handles SIG handlers
  */
$sigterm=false;
$sighup=false;

/**
  * SIG handler
  */
function sig_handler($signo)
{
	global $sigterm, $sigup;

	if($signo == SIGTERM)
		$sigterm = true;
  	else if($signo == SIGHUP)
		$sighup = true;
}

// Set up the forking
global $sigterm, $sighup;

ini_set("max_execution_time", "0");
ini_set("max_input_time", "0");
set_time_limit(0);

pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP, "sig_handler");

// fork this process into a background child process
$pid = pcntl_fork();
if($pid == -1)
	die("There is no fork()!");

if($pid)
{
	echo($pid);
	return 0;
}

while(!$sigterm)
{
	// CHANGE TO WHATEVER SCRIPT YOU WANT TO RUN
	exec('php /path/to/your/script');

	// CHANGE TO WHATEVER TIME
	sleep(2);
}

return 0;

?>

This will launch itself in the background and execute a script every x  
seconds.

HTH,

Jon.


jon gilkison
chief technology officer / massify.com


On Dec 16, 2008, at 3:45 AM, hafez ahmad wrote:

> Dears,
>
> I need to run PHP script every 10 seconds , I can do that with  
> (while true) and sleep(10), but I need to the script always run on  
> Linux machine as service.
>
> Any Ideas?
>
> Regards,
> hafez
> _______________________________________________
> New York PHP User Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> http://www.nyphp.org/show_participation.php

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20081216/51062759/attachment.html>


More information about the talk mailing list