Skip to content

Instantly share code, notes, and snippets.

@hoelzro
Created November 12, 2015 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoelzro/86ad3e184efa1042d9d0 to your computer and use it in GitHub Desktop.
Save hoelzro/86ad3e184efa1042d9d0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
use feature qw(say);
use Fcntl;
use File::Slurp qw(read_file);
my @command = @ARGV;
open my $fh, '>', '/tmp/mem-usage';
my $pid = fork();
my ( $sentinel_read, $sentinel_write );
pipe $sentinel_read, $sentinel_write;
fcntl($sentinel_write, F_SETFD, FD_CLOEXEC);
if($pid) {
close $sentinel_write;
do {
my $buffer;
sysread $sentinel_read, $buffer, 1;
};
while(1) {
my @statm = split /\s+/, scalar(read_file("/proc/$pid/statm", chomp => 1));
last if $statm[1] == 0;
say {$fh} $statm[1];
sleep 1;
}
waitpid $pid, 0;
} else {
close $sentinel_read;
exec @command;
die "couldn't execute command";
}
close $fh;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment