The Artima Developer Community
Sponsored Link

PHP Buzz Forum
PHP Command Line - Avoid several Instances of your App.

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Pure-PHP

Posts: 20
Nickname: purephp
Registered: Mar, 2005

Pure-PHP is kind of Blog about PHP, and unconventional methodes to boost PHP-apps
PHP Command Line - Avoid several Instances of your App. Posted: Mar 21, 2005 11:04 PM
Reply to this message Reply

This post originated from an RSS feed registered with PHP Buzz by Pure-PHP.
Original Post: PHP Command Line - Avoid several Instances of your App.
Feed Title: Pure PHP - PHP unconventional
Feed URL: http://www.iran-fun.com/
Feed Description: Unconventional methodes to boost php apps
Latest PHP Buzz Posts
Latest PHP Buzz Posts by Pure-PHP
Latest Posts From Pure PHP - PHP unconventional

Advertisement

Since PHP 4, you can also write command line apps in php. If you have a critical app, and you want to avoid several, or mor than one instance of your app, to run, you can use the following class. It works only on unix and linux operating sytems.

Usage:

<?php

inlude
("ProcessHandler.class.php");
if(
ProcessHandler::isActive()){
   die(
"Already running!\n";);
}else{
   
ProcessHandler::activate();
   
//run my app
}

?>

<?php

if (!defined('PID')) {
    
define("PID","/tmp/pid.php");
}
class
ProcessHandler{
    function
isActive(){
        
$pid = ProcessHandler::getPID();
        if(
$pid == null){
            
$ret = false;
        }else{
            
$ret = posix_kill ( $pid, 0 );
        }
        if(
$ret == false){
            
ProcessHandler::activate();
        }
        return
$ret;
    }
    function
activate(){
        
$pidfile = PID;
        
$pid = ProcessHandler::getPID();
        if(
$pid != null && $pid == getmypid()){
            return
"Already running!\n";
        }else{
            
$fp = fopen($pidfile,"w+");
            if(
$fp){
                if(!
fwrite($fp,"<"."?php\n\$pid = ".getmypid().";\n?".">")){
                    die(
"Can not create pid file!\n");
                }
                
fclose($fp);
            }else{
                die(
"Can not create pid file!\n");
            }
        }
    }
    function
getPID(){
        if(
file_exists(PID)){
            require(
PID);
            return
$pid;
        }else{
            return
null;
        }
    }
}

?>

Read: PHP Command Line - Avoid several Instances of your App.

Topic: Truly Functional Specs Previous Topic   Next Topic Topic: PHP Security - Ideas for Building a HttpRequest Class

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use