0

I am trying to write a shell script which will check free memory of the server. I am trying below code top -M -n1 | grep "Mem" | awk '{print 0 + $7}' | awk '{ print $1 / 1024 }' | awk '{if ($1 < 1) print "\n Available Free memory is " $1 system("sar -r") }'

I am expecting output as : -

Available Free Memory is 0.99 GB
Linux 2.6.18-274.el5 (XXXXXXXXX)      10/15/15

00:00:02    kbmemfree kbmemused  %memused kbbuffers  kbcached kbswpfree kbswpused  %swpused  kbswpcad
00:10:01      4630292  11674812     71.60    342120   8428736   4096440       124      0.00         0
00:20:01      4632356  11672748     71.59    342120   8429228   4096440       124      0.00         0
00:30:01      4595960  11709144     71.81    342128   8429712   4096440       124      0.00         0
00:40:01      4601820  11703284     71.78    342136   8430220   4096440       124      0.00         0
00:50:01      4603536  11701568     71.77    342140   8430828   4096440       124      0.00         0
01:00:01      4600656  11704448     71.78    342152   8431340   4096440       124      0.00         0
01:10:01      4606704  11698400     71.75    342156   8431868   4096440       124      0.00         0
01:20:01      4605780  11699324     71.75    342168   8432384   4096440       124      0.00         0
01:30:01      4606880  11698224     71.75    342172   8432868   4096440       124      0.00         0
01:40:01      4642516  11662588     71.53    342176   8400916   4096440       124      0.00         0
01:50:01      4642224  11662880     71.53    342180   8401400   4096440       124      0.00         0
02:00:01      4604204  11700900     71.76    342184   8402232   4096440       124      0.00         0
02:10:01      4607188  11697916     71.74    342188   8402792   4096440       124      0.00         0
02:20:01      4562004  11743100     72.02    342196   8410480   4096440       124      0.00         0

However, When i run above command, It is printing output of sar -r command first and then the line "Available Free memort is 0.99 GB

Can some one please guide me here?

Daniel t.
  • 9,291
  • 1
  • 33
  • 36
Shyam
  • 1
  • You do know of the `free` command, don't you? Also, please put a bit of effort into formatting your question in a legible fashion, using proper code blocks and such. – EEAA Oct 16 '15 at 03:29
  • I need to check all other aspects as well, like CPU utilization and Zombie process etc.. hence i used top command. Even if i use free command my question remains same :( Its Printing system("sar -r") command's output and then printing "Available free......" – Shyam Oct 16 '15 at 03:36

2 Answers2

0

You have multiple actions in your awk statement, either separate the actions with a semicolon or a new line. In this case, I have added a semicolon ; right after the end of the print statement "$1".

top -M -n1 | grep "Mem" | awk '{print 0 + $7}' | awk '{ print $1 / 1024 }' | awk '{if ($1 < 1) print "\n Available Free memory is " $1 ; system("sar -r") }'

awk syntax for a conditional with multiple actions -

if (conditional-expression)
{
    action1;
    action2;
}
Daniel t.
  • 9,291
  • 1
  • 33
  • 36
  • Thanks Daniel :) Yes, Its working now... but new problem is that - "It is executing sar -r irrespective of if condition's output" I mean, Its executing sar -r in both the cases.. if $1 < 1 its printing output of sar command and if $1 > 1 its printing output of sar command :(((((( – Shyam Oct 16 '15 at 04:11
  • Group the actions under if statement in a curly bracket then - `top -M -n1 | grep "Mem" | awk '{print 0 + $7}' | awk '{ print $1 / 1024 }' | awk '{if ($1 > 1) {print "\n Available Free memory is " $1; system("sar -r")} }'` – Daniel t. Oct 16 '15 at 22:06
0

The output is buffered in awk but not in system. In the last awk try inserting

fflush();

before the system() command:

awk '{if ($1 < 1) { print "\n Available Free memory is " $1 } fflush();  system("sar -r") }'
WeSee
  • 486
  • 1
  • 4
  • 10