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

When i use `` to invoke shell commmand, sometimes perl will failed with Argument list too long.
For example, i use
my $dir = "/slowfs/hspice01/liguoj/performance/MT4/Rundir/Mon/RHEL4"; my $begin_load = `cat /proc/loadavg`; my $space = `df $dir`;
following error info appear:
Can't exec "df": Argument list too long at /slowfs/hspice01/liguoj/workshop/bin/../lib/Runcases.pm line 365.
Can't exec "cat": Argument list too long at .....
The failed issue will not always appear.

Replies are listed 'Best First'.
Re: invoke shell with `` failed
by almut (Canon) on Apr 15, 2009 at 10:35 UTC

    Are you maybe leaving behind stuff in the environment upon every iteration?  On most Unixes, the command line arguments share a fixed space with the process' environment.

    For example, the following would reproduce the issue (tested on Linux):

    #!/usr/bin/perl for (1..10) { $ENV{JUNK} .= "ABC" x 5000; my $begin_load = `cat /proc/loadavg`; warn "cat failed: $!" if $?; print $begin_load; } __END__ 0.56 0.43 0.25 1/219 27522 0.56 0.43 0.25 3/219 27523 0.56 0.43 0.25 2/219 27524 0.56 0.43 0.25 2/219 27525 0.56 0.43 0.25 2/219 27526 0.56 0.43 0.25 2/219 27527 0.56 0.43 0.25 3/219 27528 0.56 0.43 0.25 3/219 27529 cat failed: Argument list too long at ./757540.pl line 7. cat failed: Argument list too long at ./757540.pl line 7.
      Really appreciate for your help,it should be the real reason. Is there any good method to avoid such situation since each iteration env is not the same.
Re: invoke shell with `` failed
by ikegami (Patriarch) on Apr 15, 2009 at 05:09 UTC

    Like you were told multiple times in the CB, the shell never see $dir — it sees its content — so it's useless to say `df $dir` without telling us what $dir contains.

    It seems that it contains something that's either very long or something the shells expands into something very long.

      This doesn't excuse not telling us what $dir contains (or even rule it out), but it seems the error is coming from Perl code, not from the shell. That's most curious, and I'm wondering about a false positive. But since you didn't provide us the code that issues the error, can't tell.
        following is my script for space detect here the dir is: "/slowfs/hspice01/liguoj/performance/MT4/Rundir/Tue/RHEL4", and will not change during runtime.
        detect_disk() will invoke many times, the problem is during the first several times it run correct, but then failed.
        sub detect_disk { my ($flag, $dir) = @_; my $available_space = `df $dir`; my @array = split(/\s+/, $available_space); $available_space = $array[10]; if ($flag eq 'entry' and $available_space < 6000000) { print ("NOTICE: The left space of $dir is $available_space (KB +), please make sure enough space is available for the regresssion.\n\ +n"); } elsif ($flag eq 'routine' and ($available_space < 1000000)) { print ("NOTICE: The left space of $dir is $available_space (KB +), regression will waiting!\n"); sleep 60; while ($available_space < 1000000) { # send_mail($mail_list, $report_title, $report_file); $available_space = `df $dir`; @array = split(/\s+/, $available_space); $available_space = $array[10]; sleep 60; } } }
Re: invoke shell with `` failed
by cdarke (Prior) on Apr 15, 2009 at 08:42 UTC
    This is similar to node 684300.
    Depending on the version of *NIX you are using, there are limitations on the length or number of command line arguments, set in kernel. Some platforms have a limit of 255 entries, Linux has a size limit instead which defaults to (last time I looked) 32 pages (128kb on 32-bit Intel), but this can be changed by tweeking a kernel parameter.
    Check the number, and size, of parameters being sent in the command.
      It seems my problem is not the same as node 684300. since when i use
      $begin_load = `cat /proc/loadavg`;
      I will also encounter the same issue.
        Clearly,
        my $variable = `cat some.file`;
        works fine for "well-behaved" files. I suspect the problem you are having is due to the contents of the file you are listing. Either it is too large (i.e. larger than your RAM so Perl cannot fit it into the scalar, which is unlikely) or it contains some character sequence that causes problems. Does your shell list the file OK?

        Cheers - Pat