Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

ulimit output?

by davee123 (Novice)
on Apr 01, 2008 at 20:48 UTC ( #677831=perlquestion: print w/replies, xml ) Need Help??
davee123 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write something that autodetects if the ulimit is too low when it starts by running `ulimit -n` at the start of the program, and testing to make sure that it's got enough room to open filehandles. But... the output appears to be empty? What's the deal?
bash$ ulimit -n 256 bash$ perl -e '$foo = `ulimit -n`; print $foo;' bash$

Replies are listed 'Best First'.
Re: ulimit output?
by FunkyMonk (Chancellor) on Apr 01, 2008 at 21:23 UTC
    It's because ulimit is a bash builtin. Try
    $ perl -e '$foo = qx{echo `ulimit -n`}; print $foo;' 1024

Re: ulimit output?
by Fletch (Chancellor) on Apr 01, 2008 at 21:25 UTC

    On some OSen the ulimit command is solely a shell builtin, not an external program. When you try and run it via backticks Perl is looking at your command and thinking it can cheat and run the command itself rather than invoking a shell (since it's a simple command with spaces as the only shell metacharacter). If your OS is one of those without a separate ulimit, what's really happening is that Perl tries to exec the non-existent program and fails (giving you back an undef, but you don't check for that).

    The work around is to explicitly get the shell involved, either by putting some shell metacharacters in your command (qx/ulimit -n 3>&-/) or by invoking a shell explicitly (qx/bash -c "ulimit -n"/).

    Update: Tweaked sample metacharacters in first example.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Your first example doesn't work for me:

      perl -e 'print qx/ulimit -n 2>&1/;'

      prints nothing.

      Update: Well, you update faster than I complain. Your new first example is dandy! ;-)
Re: ulimit output?
by toolic (Bishop) on Apr 01, 2008 at 21:30 UTC
    I believe it has something to do with the shell that your ulimit command is running in.

    The docs for `STRING` say:

    A string which is (possibly) interpolated and then executed as a system command with /bin/sh or its equivalent.
    You are using the bash shell, not the bourne shell (sh), and ulimit does not seem to be available in sh. If you turn on warnings, you get a little more info:
    bash-3.00$ perl -w -e '$foo = `ulimit -n`; print $foo;' Can't exec "ulimit": No such file or directory at -e line 1. Use of uninitialized value in print at -e line 1. bash-3.00$

    I'm not sure if there are other shell equivalents of ulimit or if you can fool perl into using bash somehow.

    Hope this helps.

Re: ulimit output?
by derby (Abbot) on Apr 02, 2008 at 13:28 UTC

    Rather than relying on the shell, if you have BSD::Resouce installed, you could:

    #!/usr/bin/perl use strict; use warnings; use BSD::Resource; my $max = getrlimit( RLIMIT_NOFILE ); print "$max\n";
    or if you're feeling really adventurous, with Inline::C
    #!/usr/bin/perl use Inline C; use strict; my $max = get_max_files(); print $max, "\n"; __END__ __C__ #include <sys/resource.h> int get_max_files() { struct rlimit rl; if (getrlimit( RLIMIT_NOFILE, &rl) == 0) { return rl.rlim_cur; } else { return -1; } }
    (that needs more error checking).


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://677831]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2018-03-25 00:42 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (299 votes). Check out past polls.