Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Preventing multiple instances

by stevieb (Canon)
on Dec 16, 2020 at 22:01 UTC ( #11125307=note: print w/replies, xml ) Need Help??


in reply to Preventing multiple instances

In very simple cases, such as this one, I use shared memory, or create a memory-backed directory:

sudo mkdir /var/memdir

Add the following line to the bottom of your /etc/fstab file (you'll need sudo to do this):

tmpfs /var/memdir tmpfs nodev,nosuid,size=1M 0 0

You've now created a directory, /var/memdir which only exists in memory (meaning it'll be wiped after reboot). It's only one megabyte, increase size as necessary.

Put your lock file in that directory. In simple cases, like the one you've got, I don't even write anything to the file, I just simply created it, then use exit if -e '/var/memdir/prog_name.lck';.

A more complete example:

use warnings; use strict; use File::Touch; my $lock = '/var/memdir/script.lock'; exit if -e $lock; touch($lock); # dies on error by default printf "Exists after create: %d\n", -e $lock // 0; # do stuff unlink $lock or die "Can't delete the damned lock file $lock: $!"; printf "Exists after delete: %d\n", -e $lock // 0;

Output:

spek@scelia ~/scratch $ perl script.pl Exists after create: 1 Exists after delete: 0

Replies are listed 'Best First'.
Re^2: Preventing multiple instances
by Bod (Chaplain) on Dec 17, 2020 at 23:07 UTC
    Add the following line to the bottom of your /etc/fstab file (you'll need sudo to do this):
    tmpfs /var/memdir tmpfs nodev,nosuid,size=1M 0 0

    This is a very interesting approach!
    Can you explain why you chose /var/memdir and not /tmp/memdir as it by its very nature temporary.

    Also, any reason to set the number of blocks to zero?

    Does the /etc/fstab contain all the information about the entire file systems?

      Also, any reason to set the number of blocks to zero?

      Does the /etc/fstab contain all the information about the entire file systems?

      fstab(5)

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        Thanks for the link afoken but it only answers one of my three questions...no, not the entire system is defined there but most of it is.

        Can you explain why you chose /var/memdir and not /tmp/memdir as it is by its very nature temporary.

        I'm guessing this is a design decision, not something that is going to be explained in the documentation.

        Also, any reason to set the number of blocks to zero?

        This documentation doesn't cover the size=1M 0 0 parameter and I cannot find reference to it in the links. I asked because when I looked up what these parameters do I found advice that "It is generally unwise to mount with such options" in this article.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11125307]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2021-05-08 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (96 votes). Check out past polls.

    Notices?