1

I have a system that runs different Perl scripts and I would like to intercept certain parameters of these scripts and pass them to my custom shell script.

This is what I have:

#!/usr/bin/microperl
[...]

if ($number) {
    if (&number_is_b($number)) {
        system("touch $STATE_LOCKED1");
        print 1;
    }
    else {
        system("/script/myscript.sh $number");
        print 2;
    } 
}
else {
    [...]
}

What I have added is the

system("/script/myscript.sh $number");

That calls the following shell script passing the parameter $number:

#!/bin/sh
result=$(wget -qO- http://x.x.x.x/result?qs=$1)
[...]

Now everything works fine, but I would like the original Perl script to proceed without waiting for the result of the shell script because the wget can take time. I need the Perl script to proceed autonomously without caring whether the shell completed successfully or not.

Is it possible to make it launch and some how forget about it and go on?

Do you have any suggestion on how I can do that?

Borodin
  • 126,100
  • 9
  • 70
  • 144
d82k
  • 379
  • 7
  • 17
  • +1 for "autonomously"! – Borodin Mar 10 '13 at 09:42
  • From the [Stack Overflow Perl FAQ](http://stackoverflow.com/questions/tagged/perl?sort=faq): [How can I fire and forget a process in Perl?](http://stackoverflow.com/questions/2133910/how-can-i-fire-and-forget-a-process-in-perl) – daxim Mar 11 '13 at 06:55

1 Answers1

6

One way of possibly making it work is to append an & to your command to make it run in the background. This won't always work with every version of unix though. But it's worth a try.

system("/script/myscript.sh $number &");

If this does not work, there are two other options I know of. One way is to use fork. http://perldoc.perl.org/functions/fork.html . This method can take a bit of effort.

There is also the module Proc::Background which allows you to run a process in the background in both windows and unix.

szabgab
  • 6,202
  • 11
  • 50
  • 64
Memento Mori
  • 3,327
  • 2
  • 22
  • 29
  • Hi, thank you for your quick reply. Actually the "&" seems not to work if used inside the system call. :( It works if used in command line... I will try to understand how fork can be used.. – d82k Mar 10 '13 at 10:25
  • @d82k I kept looking for more information on this since I'd have some uses for this as well. It looks like the perl module Proc::Background may actually be the ideal way. – Memento Mori Mar 10 '13 at 10:54
  • 2
    You could also `fork` and then use `exec`. – fenway Mar 10 '13 at 12:47