Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Mechanism for ensuring only one instance of a Perl script can only run?

by karlgoethebier (Abbot)
on Dec 02, 2022 at 16:48 UTC ( [id://11148505]=note: print w/replies, xml ) Need Help??

in reply to Mechanism for ensuring only one instance of a Perl script can only run?

"…a more elegant solution?"

You could take a look at your process table:

use strict; use warnings; use Proc::ProcessTable; my $processes = Proc::ProcessTable->new; for ( @{ $processes->table } ) { …; }

$_->pid and $_->cmndline might be what you want. See also

«The Crux of the Biscuit is the Apostrophe»

Replies are listed 'Best First'.
Re^2: Mechanism for ensuring only one instance of a Perl script can only run?
by afoken (Chancellor) on Dec 02, 2022 at 22:56 UTC
    my $processes = Proc::ProcessTable->new; for ( @{ $processes->table } ) { …; }

    I don't think that will always work safely:

    • At least FreeBSD does not mount /proc any more, so Proc::ProcessTable will probably return no processes at all.
    • On a system using Linux Containers, you will see processes running in containers as processes on the host. That may cause false positives.
    • You introduce a TOCTTOU problem - by the time you have evaluated data from /proc, the situation may have changed dramatically.
    • On some systems (at least Linux), content of /proc may be edited, e.g. by assigning to $0.

    Trying to locking the executable should be free of race conditions (or else flock() would be severely broken) and should also work with soft and hard links, as the file is locked, not one of its directory entries.

    Quick assign to $0 demo:

    /root>perl -E 'say `cat /proc/$$/cmdline`' perl-Esay `cat /proc/$$/cmdline` /root>perl -E '$0="find me"; say `cat /proc/$$/cmdline`' find me /root>

    pstree on a host with about 10 containers (running Proxmox VE, both host and containers using Debian 11)

    Every lxc-start is a parent process of a container, every systemd that is a child of a lxc-start is the init process (pid 1) of a container, each of those systemds and all of their children are running in a container.


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

      From README.freebsd-kvm of the current version of Proc::ProcessTable:

      "FreeBSD 5.X not mounting /proc by default. Procfs is vulnerable system and its use is not recommended in future. In addition, mapping processes space to /proc is not correct (at least, in 7 of 7 my freebsd servers with FreeBSD5 installed). So, I decided to write this code. This module works via the kvm system."

      And it should be possible to obtain the PIDs of the children of every loc-start command.

      «The Crux of the Biscuit is the Apostrophe»

      Proc::ProcessTable on FreeBSD stable/13 -- "/proc" is a plain empty directory here -- showed all the "xterm" processes using the "A cheap and sleazy version of ps" example; did not check any further.

      If anyone has suggestions for things I should check when the module may fail, I am all ears^Weyes.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-07-25 16:38 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.