[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-nano] lock a file
From: |
Trevor Cordes |
Subject: |
Re: [Help-nano] lock a file |
Date: |
Tue, 06 May 2008 07:34:57 -0500 (CDT) |
On 5 May, Justin Lecher wrote:
> Hello!
>
> Is it possible to let nano lock the file which is opened for write
> access on other session? On our server there was an accident where two
> admins has opened the same file on discarded the changes of the other
> with writing his own. Could this be avoid?
I wrote a wrapper script to do something like this. YMMV! Works for me
on recent Linux using recent perl (all Fedora 8). Just always run using
this new command and you'll always be warned!
Enjoy!
cat /usr/local/script/pc
#!/usr/bin/perl -w
# (c) 2008 Trevor Eaton Cordes address@hidden
# GPLv2, credit notice must be retained
use File::Basename;
use File::PathConvert qw(realpath);
$f=$ARGV[0] or die "Usage: pc file-to-edit\n";
$fbase=basename($f);
$freal=realpath(($f=~m#^/#?'':"$ENV{PWD}/").$f) or die;
$m='';
open(PS,'ps -efww|') or die;
while(<PS>) {
if
(m#^\S+\s+(\d+).*?\s+\d\d:\d\d:\d\d\s+nano(?:\s+-\S+)*\s+((?:/[^/]*?)*?$fbase\b)#)
{
$pid=$1;
$munreal=$2;
open(ENV,"ps --no-headers --pid $pid eww|") or die;
$mwithenv=<ENV>;
close(ENV);
($pwd)=($mwithenv=~m# PWD=(.*?)(?: ?[A-Z0-9]+=|$)#) or die "cannot find PWD
$mwithenv";
$mreal=realpath(($pwd=~m#^/#?'':"$pwd/").$munreal) or die "cannot parse
real $pwd,$munreal***,".(($pwd=~m#^/#?'':"$pwd/").$munreal).'***';
if ($freal eq $mreal) {
$m=$_;
last;
}
}
}
close(PS);
if ($m) {
($pts)=($m=~m#(pts/\d+)#) or die;
print "already editing $f on $pts\n";
print "writing ****'s to $pts\n";
$s80='*'x80;
system "echo '$s80\n$s80\n$s80' | write $ENV{LOGNAME} $pts";
exit 1;
}
exec 'nano','-z','-w',$f;