How to Set a Ulimit from Inside a Perl Script That Applies to Its Children

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

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.

How can I specify timeout limit for Perl system call?

See the alarm function. Example from pod:

eval {
local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
alarm $timeout;
$nread = sysread SOCKET, $buffer, $size;
alarm 0;
};
if ($@) {
die unless $@ eq "alarm\n"; # propagate unexpected errors
# timed out
}
else {
# didn't
}

There are modules on CPAN which wrap these up a bit more nicely, for eg: Time::Out

use Time::Out qw(timeout) ;

timeout $nb_secs => sub {
# your code goes were and will be interrupted if it runs
# for more than $nb_secs seconds.
};

if ($@){
# operation timed-out
}

How do you prevent perl from making a dump file?

To prevent the creation of a core dump file from within the perl script, you can use (procedure stolen from How do I set a ulimit from inside a Perl script that applies to its children?):

require 'syscall.ph';
require 'sys/resource.ph';
$rstruct = pack "L!L!",0,&RLIM_INFINITY;
syscall(&SYS_setrlimit,&RLIMIT_CORE,$rstruct);

To limit dumps to a certain size, change the 0 above to the desired size.

Perl IPC::Run3: How to emulate a pty for stdin to child process?

who -m specifically gives information about the terminal connected to its stdin.

-m     only hostname and user associated with stdin

You've replaced the terminal, so you can't obtain information from it.

$ who -m
ikegami pts/1 ...

$ who -m </dev/null

$

You could drop the -m and use just who.

$ who </dev/null
ikegami pts/1 ...

Alternatively, you could use $ENV{USER} or getpwuid($>) (the name of the user as which the process is executing) instead.

Where to put Perl script in Laravel project

I.e. you can create a subdirectory, let's say scripts, in the Laravel's storage directory and put your perl script(s) in there.

Then you can schedule its execution like this

$schedule->exec('/usr/bin/perl '.storage_path('scripts/myscript.pl'))
->daily()
->at('09:00');


Related Topics



Leave a reply



Submit