system @args problem
qiaojy
created: 2006-01-18 00:10:37
I use "system @args" to run a seperate program in Perl script and get the return by "$rc=Oxffff & system @args". However, sometimes the Perl script cannot notice the sub-process has done and get its return value. The Perl script just hang on there and wait till I kill it. Does anybody know what the problem is?
Re: system @args problem
created: 2006-01-18 00:35:52

It would help to know what program and arguments you are passing, and if it always occurs with one/a few programs in particular, or all that you try it with.

Some reasons I can think of off-hand include input is expected that isn't present, the process is taking longer than expected, resource limitations, the program doesn't like being run in that manner, or something similar-but without knowing more detail, it is hard to do more than just guess.

Re: system @args problem
created: 2006-01-18 02:41:03
The usual way to deal with something like this is to wrap your system call in an [doc://eval] block, and use an [doc://alarm] to set a timeout. For example:
my $timeout = 20;   # or whatever is appropriate

eval {
    local $SIG{ALRM} = sub { die "timedout" };
    alarm $timeout;
    # Your system call goes here....
    alarm 0;
};

if ($@) {
    #  $@ will contain error message, if any
}

Hope this helps,
Darren :)

Re: system @args problem
created: 2006-01-18 03:10:51

I doubt that the script "cannot notice the sub-process has done". More probably the process forked off with system does never actually return. In which case you may set up a timeout. This is usually done through eval and alarm. You should get plenty of examples by doing a search, so it's not worth repeating any here. If you have any specific problem, then ask again.

PS: now that we told you 'bout eval, please use it only as necessary, and do not abuse it, especially in its string form, it's often frowned upon as Bad(TM), and there are good reasons why it is!

perlmonks.org content © perlmonks.org and atcroft, blazar, McDarren, qiaojy

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03