Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Finding out if another copy of a CGI Perl scripts is running?

Reply
Thread Tools

Finding out if another copy of a CGI Perl scripts is running?

 
 
Lisa
Guest
Posts: n/a
 
      12-18-2004
How would one go about finding out if another copy of a CGI Perl
scripts is running?

I want to set up a script to run as a cron job, starting about once a
minute, however, the first thing I want to do when the script starts is
to check and see if the last incarnation is still (highly unlikly but
could happen on very busy days) running and shut down if it is, so that
there is only one version of the script proccessing data.

Lisa

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      12-18-2004
Lisa wrote:

> How would one go about finding out if another copy of a CGI Perl
> scripts is running?


Have it lock something.

> I want to set up a script to run as a cron job,


Huh? I thought we were talking about CGI scripts, not cron scripts.
Not of course that it makes any difference.

> starting about once a
> minute, however, the first thing I want to do when the script starts is
> to check and see if the last incarnation is still (highly unlikly but
> could happen on very busy days) running and shut down if it is, so that
> there is only one version of the script proccessing data.


In that case, the data would seem the obvious thing to lock.

 
Reply With Quote
 
 
 
 
Alan Mead
Guest
Posts: n/a
 
      12-18-2004
Star date: Sat, 18 Dec 2004 15:00:23 -0800, Lisa's log:


> I want to set up a script to run as a cron job, starting about once a
> minute, however, the first thing I want to do when the script starts is
> to check and see if the last incarnation is still (highly unlikly but
> could happen on very busy days) running and shut down if it is, so that
> there is only one version of the script proccessing data.


First, if it runs from a cron job then it's just a script. CGI is a way
that scripts generate dynamic web pages.

There are probably several ways you can do this. An obvious one would be
to make a "lock file"... if your script detects the lock file then either
(1) there is a copy running or (2) a copy died leaving a stale
lock.

I've never done this in Perl, but I bet you could grep the running
processes for your script name (e.g., 'my $rs = `ps -aux | grep
[m]yscript.pl`'). Of course, you'd have to ignore the currently running
version.
 
Reply With Quote
 
Lisa
Guest
Posts: n/a
 
      12-18-2004
OK, but I do not want the second iteration to wait for a lock I want it
to recognize the existance of the previously running version still
being active and to exit gracefully.

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-18-2004
Lisa wrote:
> OK, but I do not want the second iteration to wait for a lock I want it
> to recognize the existance of the previously running version still
> being active and to exit gracefully.


So don't have it wait, but have it exit if the file is locked.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Alan Mead
Guest
Posts: n/a
 
      12-18-2004
Star date: Sat, 18 Dec 2004 15:45:03 -0800, Lisa's log:

> OK, but I do not want the second iteration to wait for a lock I want it
> to recognize the existance of the previously running version still
> being active and to exit gracefully.


exit if ( -x $lockfile );

'-x' is from memory...

 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      12-19-2004
On Sat, 18 Dec 2004, Alan Mead wrote:

> First, if it runs from a cron job then it's just a script.


agreed

> CGI is a way that scripts generate dynamic web pages.


CGI is a software interface between a web server and a script.

What you do with it beyond that is... well, limited only by the
designer's imagination...

I don't know about you, but to me the term "dynamic web pages" could
cover a wide multitude of sins. Many of which don't involve CGI.

But yes, you could dynamically create web pages. Creating "dynamic
web pages" isn't quite the same thing, though.

SCNR.
 
Reply With Quote
 
KKramsch
Guest
Posts: n/a
 
      12-19-2004
In < .com> "Lisa" <> writes:

>OK, but I do not want the second iteration to wait for a lock I want it
>to recognize the existance of the previously running version still
>being active and to exit gracefully.


Then request a non-blocking lock:

open MY_DATA, $datafile or die "Couldn't read $datafile\n";
exit GRACEFULLY unless flock(MY_DATA, LOCK_EX|LOCK_NB);

See The Perl Cookbook pp. 245-247.

Karl
--
Sent from a spam-bucket account; I check it once in a blue moon. If
you still want to e-mail me, cut out the extension from my address,
and make the obvious substitutions on what's left.
 
Reply With Quote
 
Lisa
Guest
Posts: n/a
 
      12-19-2004
Here is what I have been trying and it does not work as I expexcted it
to.

if(-e "LockFile.txt"){
print "LockFile already exists",br();
exit;
}else{
if(open("LOCKFILE",">LockFile.txt")){
print "opening LockFile",br();
print LOCKFILE "1";
close LOCKFILE;
}else{
die "Can't open LockFile";
}
}

sleep(20)
unlink("LockFile.txt");

print "Done",br();
exit


If I comment out the sleep and unlink commands, and let the script end
without deleting the file, then the next run of the script finds the
file and exits. However if I leave the sleep unlink in the script and
run two iterations of the script they both run instead of the second
one exiting because the first one is sleeping and has not unlinked the
lock file as I expect it to.

Lisa

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      12-19-2004
Alan Mead wrote:

> I've never done this in Perl, but I bet you could grep the running
> processes for your script name (e.g., 'my $rs = `ps -aux | grep
> [m]yscript.pl`'). Of course, you'd have to ignore the currently running
> version.


I have an extremely NON-portable version that works on Linux.
-Joe

#!/usr/bin/perl
# Purpose: Starts a command only if it is not already running

use strict; use warnings;

my $Usage = "Usage: $0 command args\n";
# Runs command if not already running
my @cmd = @ARGV or die $Usage;
my $cmd_line = "@cmd";
die "Command line too long: $cmd_line\n" if length $cmd_line > 4095;

my ($user,$header) = get_uid_from_ps();
my $count = 0;
foreach (`ps -efww`) { # This has a limit of 4096 bytes for CMD
my ($who,$pid,$cmd) = (split /\s+/,$_,7)[0,1,6];
next unless $who eq $user and $cmd =~ /\Q$cmd_line\E$/;
next if $pid == $$;
print STDERR " $user is running '$cmd_line'\n$header" unless $count++;
print STDERR $_;
}
print STDERR " count = $count\n" if $count > 1;
exit 1 if $count;
print "$cmd_line\n";
exec @cmd;

sub get_uid_from_ps {
my @ps = `ps -fp $$`;
my @cols = split ' ',$ps[0];
$_ = "UID PID PPID C STIME TTY TIME CMD";
die "'@cols' != '$_'" unless "@cols" eq $_;
@cols = split ' ',$ps[1];
($cols[0], $ps[0]);
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Python Scripts with IIS - ASP or Python-based CGI scripts withIIS - which makes more sense? davidj411 Python 0 06-27-2008 04:38 PM
What is required for perl scripts to run correct when launched from rc scripts on HPUX 11? deanjones7@gmail.com Perl Misc 13 09-10-2007 11:58 AM
what's wrong calling a Perl/CGI script in Perl/CGI script under Tomcat server? kath Perl Misc 4 04-09-2007 09:21 PM
File Creation Problem with CGI Scripts in Apache cgi-bin (Fedora Core 3) BestFriend Perl Misc 2 08-21-2006 04:02 PM
CGI Programmers needed to hack around at our CGI Scripts James Perl Misc 1 08-04-2003 09:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57