note 93753 added to function.getmypid

From: Date: Fri, 25 Sep 2009 20:51:56 +0000
Subject: note 93753 added to function.getmypid
Groups: php.notes 
Request: Send a blank email to php-notes+get-161087@lists.php.net to get a copy of this message
Looking to create a lock-file mechanism for a cmd-line script? Enjoy! #!/usr/bin/php <? define( 'LOCK_FILE', "/var/run/".basename( $argv[0], ".php" ).".lock" ); if( isLocked() ) die( "Already running.\n" ); # The rest of your script goes here.... echo "Hello world!\n"; sleep(30); unlink( LOCK_FILE ); exit(0); function isLocked() { # If lock file exists, check if stale. If exists and is not stale, return TRUE # Else, create lock file and return FALSE. if( file_exists( LOCK_FILE ) ) { # check if it's stale $lockingPID = trim( file_get_contents( LOCK_FILE ) ); # Get all active PIDs. $pids = explode( "\n", trim( ps -e | awk '{print $1}' ) ); # If PID is still active, return true if( in_array( $lockingPID, $pids ) ) return true; # Lock-file is stale, so kill it. Then move on to re-creating it. echo "Removing stale lock file.\n"; unlink( LOCK_FILE ); } file_put_contents( LOCK_FILE, getmypid() . "\n" ); return false; } ---- Server IP: 72.55.140.6 Probable Submitter: 96.48.169.203 ---- Manual Page -- http://www.php.net/manual/en/function.getmypid.php Edit -- https://master.php.net/note/edit/93753 Del: integrated -- https://master.php.net/note/delete/93753/integrated Del: useless -- https://master.php.net/note/delete/93753/useless Del: bad code -- https://master.php.net/note/delete/93753/bad+code Del: spam -- https://master.php.net/note/delete/93753/spam Del: non-english -- https://master.php.net/note/delete/93753/non-english Del: in docs -- https://master.php.net/note/delete/93753/in+docs Del: other reasons-- https://master.php.net/note/delete/93753 Reject -- https://master.php.net/note/reject/93753 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#161087) next »