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
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
.)
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.
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.
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.