<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Colocation to Virtualization &#187; Programming</title>
	<atom:link href="http://blog.colovirt.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.colovirt.com</link>
	<description>and linux between</description>
	<lastBuildDate>Thu, 05 Jan 2012 08:00:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.colovirt.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/afd0d00d02b5abc67acf29066e3d1e3b?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Colocation to Virtualization &#187; Programming</title>
		<link>http://blog.colovirt.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.colovirt.com/osd.xml" title="Colocation to Virtualization" />
	<atom:link rel='hub' href='http://blog.colovirt.com/?pushpress=hub'/>
		<item>
		<title>Programming/Linux: Perl Implementing Lockfiles With Syslog</title>
		<link>http://blog.colovirt.com/2009/02/05/programminglinux-implementing-lockfiles/</link>
		<comments>http://blog.colovirt.com/2009/02/05/programminglinux-implementing-lockfiles/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 08:11:13 +0000</pubDate>
		<dc:creator>Kevin Goodman</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[checklock]]></category>
		<category><![CDATA[die]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[facility]]></category>
		<category><![CDATA[LinkedIn]]></category>
		<category><![CDATA[lock file]]></category>
		<category><![CDATA[lockfile]]></category>
		<category><![CDATA[LockFile::Simple]]></category>
		<category><![CDATA[locl]]></category>
		<category><![CDATA[logger]]></category>
		<category><![CDATA[logger::syslog]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[simple]]></category>
		<category><![CDATA[sub]]></category>
		<category><![CDATA[subroutine]]></category>
		<category><![CDATA[syslog]]></category>
		<category><![CDATA[trylock]]></category>
		<category><![CDATA[unlock]]></category>

		<guid isPermaLink="false">http://blog.colovirt.com/?p=407</guid>
		<description><![CDATA[Programming/Linux: Perl Implementing Lockfiles With Syslog.  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.colovirt.com&amp;blog=5256186&amp;post=407&amp;subd=colovirt&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The perl script below will outline a general process of implementing<strong> lock files</strong>.  Lock files here are used to check if another instance of the script is currently running.  This can also be accomplished by interfacing with process listings or writing your own file checking code. Utilizing<strong> LockFile::Simple</strong> so far seems to be the cleanest/fastest way that I have found to achieve this.</p>
<p>Currently, most of the perl scripts I write are scheduled via cron to run.  The number of files to be processed fluctuates between small to huge.  Writing in a lock file operation keeps the script from adversely interacting with the files that another instance is still processing.  In this case, the newer executed script will error and allow the previous to complete it&#8217;s execution.</p>
<p><span id="more-407"></span><strong>Example Script:</strong></p>
<pre>#!/usr/bin/perl

# Facility to send errors to syslog.
use Logger::Syslog;

# Nice little module for handling lockfiles
use LockFile::Simple qw(lock trylock unlock);

# Define location for the lockfile to reside. The filname
# will have .lock appended to the end automatically.
$lockf = '/home/user/lock/filelock';

# Define a sub-routine to check the lock on script start
checklock();

# Start the main process of the script if no other instance
# is running.
dosomething();

# If dosomenthing() executes correctly, log to syslog for tracking.
error("SERVICE:CHECK - Check Completed");

# Now remove the lockfile so future instances will be able to start.
unlock("$lockf");

sub checklock {
	# If lock file already exists jump to
	# die_nice() subroutine passing value of 1
        die_nice(1)  unless trylock("$lockf");
}

# Define what you want the program to do if another instance
# is not running.
sub dosomething {
		blah blah blah blah blah blah
                do stuff here
}

sub die_nice {
#
# die_nice sub routine is used for better error logging and event handling
# then the standard die() function.
#
# Error         Description : Source
# 1     -       file is locked : checklock()
#
        $inc = @_[0];
        if ($inc == 1) {
		# If die_nice was passed with 1, another instance is running
		# and report back to syslog the following error.
                error("SERVICE:CHECK - Check could not run! intervention needed.
                        Another instance has file locked err 1");
		# Kill current process of this perl script.
                die();
        }
}</pre>
<p><strong>Note: This solution is working fine for me currently.  Let me know of any other easier way.  This works great when doing centralized syslogging and alerting from that.  And yes, I know &#8220;blah blah blah blah blah blah&#8221; is not vaild perl code :)</strong></p>
<br />Posted in Linux, Programming  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/colovirt.wordpress.com/407/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/colovirt.wordpress.com/407/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/colovirt.wordpress.com/407/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/colovirt.wordpress.com/407/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/colovirt.wordpress.com/407/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/colovirt.wordpress.com/407/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/colovirt.wordpress.com/407/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/colovirt.wordpress.com/407/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/colovirt.wordpress.com/407/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/colovirt.wordpress.com/407/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/colovirt.wordpress.com/407/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/colovirt.wordpress.com/407/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/colovirt.wordpress.com/407/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/colovirt.wordpress.com/407/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.colovirt.com&amp;blog=5256186&amp;post=407&amp;subd=colovirt&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.colovirt.com/2009/02/05/programminglinux-implementing-lockfiles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Kevin Goodman</media:title>
		</media:content>
	</item>
	</channel>
</rss>
