Home > Software design >  Processes that use more than 1GB of RAM
Processes that use more than 1GB of RAM

Time:08-25

I have a command that displays how much RAM the processes are using:

ps aux | awk '{print $6/1024 " MB\t\t" $11}' | sort -n

How can I modify the command to show processes that are using more than 1GB of RAM?

CodePudding user response:

I took your command, added a ~ to split on, piped it to a while loop, split on the ~ to get just the number of MBs, cut off the decimal place, then checked to see if the MB was over a cutoff value, and if it was i printed the full line with the process name. finally i removed the added ~ and sorted.

#!/bin/bash

CUTOFF=1024

ps aux | awk '{print $6/1024 "~ MB\t\t" $11}' | while IFS=\n read PROCESS; do
  MEMORY=`echo $PROCESS | cut -d'~' -f1 | cut -d'.' -f1`
  if [[ $MEMORY -gt $CUTOFF ]]; then
    echo $PROCESS | sed 's/~//'
  fi
done | sort -n

and you can run it from the command line by adding a few ;s

$ CUTOFF=1024; ps aux | awk '{print $6/1024 "~ MB\t\t" $11}' | while IFS=\n read PROCESS; do MEMORY=`echo $PROCESS | cut -d'~' -f1 | cut -d'.' -f1`; if [[ $MEMORY -gt $CUTOFF ]]; then echo $PROCESS | sed 's/~//'; fi; done | sort -n

Let me know if this works for you or if you have any questions.

  •  Tags:  
  • bash
  • Related