I know that this question has been asked 500 times over the last dozen or more years and I've read most of the answers. Since I'm writing this, the answers don't do what I need. Or I haven't found the One True Answer.

Design goal #1: Write a Perl script that runs on any platform that supports Perl without special casing.

Design goal #2: Use the minimum number of manually added packages as possible.

The script runs through a list of possible jobs to do. When it (A) finds one, it 'locks' that job and spawns another instance of the script (B) to continue looking for jobs. When A finishes, it unlocks the job and dies. B becomes the only instance running (the new A). Lather, rinse, repeat.

Question: What is a single platform independent way of spawning another instance of the running script?

The code below needs to be saved as "spawn.pl"

#!/usr/bin/perl use warnings; use strict; use Proc::Background; my $arg = shift @ARGV; if (!defined ($arg)) { print ("Usage: \"perl spawn.pl n\" where n is the number of times +to spawn\n"); exit (0); } my $c = $arg + 0; print ("Starting PID $$ as instance $c\n"); if ($c > 0) { $c--; sleep (2); if (1) { # # This only works on Windows (as is well noted) # system (1, "perl spawn.pl $c"); } elsif (0) { # # This does not work on Windows because it doesn't have the fu +ll path # Same on Linux Mint (Ubuntu or Debian) # my $arg1 = "$c"; # my $command = "perl spawn.pl"; my $command = "spawn.pl"; my $proc1 = Proc::Background->new ($command, $arg1); } elsif (0) { # # Does not work on Windows because it waits for completion # Works on Linux Mint # system ("perl spawn.pl $c &"); } else { # # Need something here # } } sleep (3); print ("PID $$ ended\n"); exit (0);

In reply to Yet another spawn question. Portable method? by MickeyLane

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.