Sys::RunAlways - make sure there is always one invocation of a script active


Sys-RunAlways documentation Contained in the Sys-RunAlways distribution.

Index


Code Index:

NAME

Top

Sys::RunAlways - make sure there is always one invocation of a script active

SYNOPSIS

Top

 use Sys::RunAlways;
 # code of which there must always be on instance running on system

DESCRIPTION

Top

Provide a simple way to make sure the script from which this module is loaded, is always running on the server.

VERSION

Top

This documentation describes version 0.04.

METHODS

Top

There are no methods.

THEORY OF OPERATION

Top

The functionality of this module depends on the availability of the DATA handle in the script from which this module is called (more specifically: in the "main" namespace).

At INIT time, it is checked whethere there is a DATA handle: if not, it exits with an error message on STDERR and an exit value of 2.

If the DATA handle is available, and it cannot be flocked, it exits silently with an exit value of 0.

If there is a DATA handle, and it could be flocked, a message is put on STDERR and execution continues without any further interference.

REQUIRED MODULES

Top

 Fcntl (any)

CAVEATS

Top

changing a running script

If you change the script while it is running, the script will effectively lose its lock on the file. Causing any subsequent run of the same script to be successful, causing two instances of the same script to run at the same time (which is what you wanted to prevent by using Sys::RunAlone in the first place). Therefore, make sure that no instances of the script are running (and won't be started by cronjobs while making changes) if you really want to be 100% sure that only one instance of the script is running at the same time.

ACKNOWLEDGEMENTS

Top

Inspired by Randal Schwartz's mention of using the DATA handle as a semaphore on the London PM mailing list.

SEE ALSO

Top

Sys::RunAlone.

AUTHOR

Top

 Elizabeth Mattijsen

COPYRIGHT

Top


Sys-RunAlways documentation Contained in the Sys-RunAlways distribution.

package Sys::RunAlways;

# version info
$VERSION = '0.04';

# be as strict and verbose as possible
use strict;
use warnings;

# make sure we know how to lock
use Fcntl ':flock';

# satisfy -require-
1;

#---------------------------------------------------------------------------
#
# Standard Perl functionality
#
#---------------------------------------------------------------------------

INIT {
    # no warnings here
    no warnings;

    # no data handle, we're screwed
    print( STDERR "Add __END__ to end of script '$0' to be able use the features of Sys::RunAlways\n" ),exit 2
     if tell( *main::DATA ) == -1;

    # we're still running
    exit 0 if !flock main::DATA,LOCK_EX | LOCK_NB;

    # we're starting
    print( STDERR "'$0' has been started at ".(scalar time)."\n");
} #INIT

#---------------------------------------------------------------------------

__END__