Search code examples
bashawkps

get memory from ps and compare via bash with my limit


I have code that I use in openwrt. I need to check memory which use application

#!/bin/bash
VAR=$(ps | grep sca | grep start | awk '{print $3}')
VAG=$(cat /proc/pid/status | grep -e ^VmSize | awk '{print $2}')
if [ $VAG>28000 ]
then
echo test
fi

No matter if I use VAR or VEG(for example VAR/VAG equal 15000), I can get work this code. I always get "test"


Solution

  • Your if statement is incorrect. The test command (aka [) must receive separate arguments for the operands and the operator. Also, > is for string comparisons; you need to use -gt instead.

    if [ "$VAG" -gt 28000 ]
    

    Since you are using bash, you can use the more readable arithmetic command instead of [:

    if (( VAG > 28000 ))