PugSA has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I am writing a script that determines backups that needs to be done on seperate servers. After certain crteria was met it needs to execute the backup software and go on to the next server. The backup should run simultaniously but I can't seem to find a way to execute the backup.pl and go on with the script.

I've written and example of what the script should do in the following three test scripts

Altough the tests are written in windows Perl the script will run on Linux

You will find in the exec command in example below the script runs the test2.pl but not the test3.pl

Please Help

test.pl

#!c:\perl\bin eval{exec("test2.pl");}; eval{exec("test3.pl");}; print "done\n\n";

test2.pl

#!c:\perl\bin sleep(10); open(TEST,">test.log"); print TEST "hello world\n"; close TEST;

test3.pl

#!c:\perl\bin sleep(10); open(TEST,">test2.log"); print TEST "Hello World\n"; close TEST;

Replies are listed 'Best First'.
Re: execute secondary script not waiting for a result and continue with script
by moritz (Cardinal) on May 30, 2007 at 09:42 UTC
    exec executes a script and never returns.

    You may want to fork() before you exec, and only exec in one of the branches.

      You may want to fork() before you exec, and only exec in one of the branches.

      It's a funny coincidence that something very incisive to this effect, although focusing not as much on Perl functions as on the corresponding system calls, was written here quite recently. I don't know why there's this common misconception about exec: perhaps it's simply because of the suggestion given by its own name. To stress the concept, fork and exec each do very simple and not terribly useful things: but put together they act synergetically to do something very useful. In contrast under some OSen and programming languages based on them to do the same requires calling functions with really complex APIs involving dozens of parameters...

Re: execute secondary script not waiting for a result and continue with script
by clinton (Priest) on May 30, 2007 at 09:57 UTC
    exec executes your command test2.pl and never returns to your script. You probably want system instead.

      I need test2.pl & test3.pl to run at the same time, with system() it will wait for test2.pl to finish before executing test3.pl

        the easy way is to use the shell to run the command on the background:
        system("test2.pl &");
        Another option is to use fork to create new processes and then exec to call the other script:
        my $pid = fork; if (defined $pid and !$pid) { exec "test2.pl"; exit(0); # just in case exec fails! }
        I need test2.pl & test3.pl to run at the same time, with system() it will wait for test2.pl to finish before executing test3.pl

        If you don't want to explicitly fork as correctly suggested to you by several monks, then a cheap way to have perl implicitly do that for you that is often is by means of piped open's.