How do I set a ulimit from inside a Perl script that applies to its children?

后端 未结 4 2012
花落未央
花落未央 2021-01-02 16:00

I have a Perl script that does various installation steps to set up a development box for our company. It runs various shell scripts, some of which crash due to lower than r

相关标签:
4条回答
  • 2021-01-02 16:12

    You've already answered your question: use BSD::Resource.

    There isn't anything in the Perl core that interfaces with setrlimit. If you can't (or won't) use the standard method, then you have to use a hack. Any of the methods you've already described would work. (Note that you could create a subroutine to prepend ulimit -s BLA; to every command, and then use that sub instead of system.)

    0 讨论(0)
  • 2021-01-02 16:12

    I ended up prepending ulimit -s BLA to the commands that needed it. I specifically didn't want to go with BSD::Resource because it's not a default Perl package and was missing on about half of the existing dev machines. No user interaction was a specific requirement.

    0 讨论(0)
  • 2021-01-02 16:26

    You can always wrap your perl in a little shell script:

    #!/bin/sh -- # --*-Perl-*--
    ulimit -n 2048
    exec /usr/bin/perl -x -S $0 ${1+"$@"}
    #!/usr/bin/perl
    #line 6
    
    use strict;
    
    # etc, etc....
    

    It's ugly, and obviously, script start up time will be slightly longer.

    0 讨论(0)
  • 2021-01-02 16:33

    Here's an example of how to set the cpu limit without using BSD::Resource (but assuming the perl system headers are there). To adapt to other resources, make the obvious changes.

    require 'syscall.ph';
    require 'sys/resource.ph';
    
    # set the soft cpu limit to 1 (second), and the hard limit to 10.
    $rstruct = pack "L!L!",1,10; # L! means native long unsigned int.
    syscall(&SYS_setrlimit,&RLIMIT_CPU,$rstruct);
    

    This assumes knowledge that rlim_t is in fact unsigned long; I don't know if there's a way to extract this info from the Perl headers.

    0 讨论(0)
提交回复
热议问题