#!/usr/bin/perl use strict; use warnings; my $memlimit = "25.0"; # maximum percentage of memory user can use my $cpulimit = "25.0"; # maximum percentage of CPU user can use my $username = `whoami`; my ($usermem, $usercpu) = getUserMemCPUperc( '$username' ); if (($usermem < $memlimit) && ($usercpu < $cpulimit)){ # do stuff! } else { # memory or CPU limits reached exit(0); } exit(0); sub getUserMemCPUperc { my( $username ) = shift; my $top = `top -b -n 1 -u $username`; # gets top from the system my ($cpu, $mem); $_ = $top; # @processes =~ /.../g; # regex to extract each process line # foreach my $process (@processes) { # my ($procpu, $procmem) =~ /.../; # get cpu and memory percent # $cpu += $procpu; # add process cpu percent to total # $mem += $procmem; # add process memory percent to total # } # return ($mem, $cpu); }