#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    7
    Rep Power
    0

    How to set an ulimit -n(open files) with perl programming


    How to set an ulimit -n(open files) with Perl programming. Since ulimit is a internal shell command and Perl cannot invoke by system or ` `. Please help me with your suggestions.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    You can call shell internal commands, as shown in this session under the Perl debugger.

    Perl Code:
      DB<1> $c = `pwd`
     
      DB<2> print $c
    /home/User
     
      DB<3>
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    7
    Rep Power
    0
    thanks Laurent_R for helping on this.

    I just tried to set the the ulimit -n (open files size) to 10240 like below piece of code. but it is reflecting in the terminal session.

    my $nag = `ulimit -n 10240`;
    print "$nag";

    NOTE: Default value of ulimit -n size is 256 and also user has sudo permissions(Admin privileges).It is happening through command prompt not working through script.

    please guide me if we can do this through script.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,873
    Rep Power
    1225
    You haven't clearly stated it, but it appears that both of your questions involve attempting to alter the environment of the parent process, which as I mentioned in your other question, can't be done in the Perl script.

    Please explain what you're needing to accomplish which requires these environment changes.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    Originally Posted by FishMonger
    You haven't clearly stated it, but it appears that both of your questions involve attempting to alter the environment of the parent process, which as I mentioned in your other question, can't be done in the Perl script.
    Yes, Fishmonger, I also strongly suspected that this was the aim, but I just wanted to point out that the starting point of the OP's description of the issue was incorrect: it is perfectly possible call shell internal commands (pwd, kill, echo, cd, ls, etc.). But the result is not always what is expected (as can be seen below).

    To the OP: why is it then impossible to change environment variables or modify the environment with a ulimit command? The main reason is that when you try to "shell out" to do that with a system or a `...` command, Perl is is forking, i.e. is creating a new sub-process, and any environment change that you do in that sub-process might be seen only within that sub-process, but not in the parent process once the sup-process has died.

    Just another example under the debugger of the nature of the problem:

    Perl Code:
      DB<1> print `pwd`
    /home/User
     
      DB<2> `cd ..`
     
      DB<3> print `pwd`
    /home/User


    The program apparently did not move up one directory. In fact, it did. But once the thread died, we were back in the original execution Perl environment.

    Just a last example, in fact almost the same with a slight variation:

    Perl Code:
      DB<4> $c = `pwd`
     
      DB<5> print $c
    /home/User
     
      DB<6> `cd ..; ls; pwd`
     
      DB<7> @d = `cd ..; ls`
     
      DB<8> x @d
    0  'User
    '
    1  '/home
    '
     
      DB<9> print `pwd`
    /home/User


    Instructions 4 and 5 are the same we have already seen.

    Instruction 6 does something more complex: move up one directory and list the content and print the current directory. THIS WORKS: the @d variable shows that ls command actually does list the content of the directory above (in this case, there is only on entry in the /home) and then displays the current directory.

    But once this is finishes, we are back where we were.

    Similarly, you might launch a complete shell script from a system or `...` (or an executable program, or whatever), but changes to the execution environment will be seen only within that sub-process. But some changes made by that script will be permanent: say, if you delete a file in this script, clearly, the file will be no longer there.

    Well, it is slightly more complicated in reality and there may be in some cases ways of getting around that (this is rarely very pretty programming, but sometimes you have to live with that), but this is really getting into shell programming rather than Perl programming, which is not the topic of this forum, and we don't know really what you want to do.

    As an example, you could run from Perl a shell script that changes the ulimit thing and happily runs with that changed environment. Sometimes, it is all what you need. But, by now, I hope that you understand that these changes will generally not apply to the parent Perl process.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    7
    Rep Power
    0
    thanks for the help!!

IMN logo majestic logo threadwatch logo seochat tools logo