0

i want to print the memory of process id's. But, i am getting error in if block as it is unable to check for the string as integer is expected.

printf "%-10s%-15s%-15s%s\n" "PID" "OWNER" "MEMORY" "COMMAND"


function sysmon_main(){
RAWIN=$(ps -o  pid,user,%mem,command ax |grep -v PID |awk '/[0-9]*/{print $1 ":" $2 ":" $4}')


for i in $RAWIN
do
     PID=$(echo $i | cut -d: -f1)
     OWNER=$(echo $i| cut -d: -f2)
     COMMAND=$(echo $i| cut -d: -f3)
     MEMORY=$(pmap $PID | tail -n 1| awk '/[0-9]K/{print $2}')

if [ "$MEMORY" -gt 0 ];then

     printf "%-10s%-15s%-15s%s\n" "$PID" "$OWNER" "$MEMORY" "$COMMAND"
fi
done
}

sysmon_main | sort -bnr -k3

I'm getting below error

./sysmon: line 17: [: 0K: integer expression expected
./sysmon: line 17: [: 126704K: integer expression expected
./sysmon: line 17: [: 14216K: integer expression expected
./sysmon: line 17: [: 48187132K: integer expression expected
2
  • You need to remove that K at the end of the string. Read up on parameter expansion in the bash manual for some ways to do that. Commented Dec 8, 2020 at 8:19
  • I'm still getting the error even though i remove the K Commented Dec 8, 2020 at 9:10

1 Answer 1

1

You may use this script:


while read -r pid owner m cmd; do
   if [[ $pid =~ ^[0-9]+$ && $cmd != "ps -o"* ]]; then
      mem="$(pmap $pid | awk 'END {print $NF+0}')"
      ((mem > 0)) && printf "%-10s%-15s%-15s%s\n" "$pid" "$owner" "$mem" "$cmd"
   fi
done < <(ps -o  pid,user,%mem,command ax)

Sign up to request clarification or add additional context in comments.

3 Comments

How to convert the above mem to represent in GB?
i'm getting error as below. I believe it is because it is unable to take the float value ./sysmon: line 34: ((: 0.12281: syntax error: invalid arithmetic operator (error token is ".12281")
ok try: pmap $pid | awk 'END {print int(($NF+0)/1024*1024)}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.