Created
April 11, 2013 02:22
-
-
Save mjdominus/5360158 to your computer and use it in GitHub Desktop.
with-memory-limit utility program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| !/usr/bin/perl | |
| use BSD::Resource qw(setrlimit RLIMIT_VMEM RLIM_INFINITY); | |
| my ($mem) = shift // usage(); | |
| @ARGV or usage(); | |
| my ($n, $s) = $mem =~ /\A (\d+) ([kmgKMG]?) \z/x or usage(); | |
| my $suf = { k => 1024, | |
| m => 1024 * 1024, | |
| g => 1024 * 1024 * 1024, | |
| }; | |
| if ($s) { | |
| if (exists $suf->{lc $s}) { | |
| $n *= $suf->{lc $s}; | |
| } else { | |
| usage(); | |
| } | |
| } | |
| setrlimit(RLIMIT_VMEM, $n , RLIM_INFINITY) or die "setrlimit: $!"; | |
| exec @ARGV; | |
| die "$ARGV[0]: $!\n"; | |
| sub usage { | |
| die "usage: with-memory-limit AMOUNT command args... | |
| AMOUNT is a number with an optional suffix of k, m, or g.\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment