0

I know how to get the total memory in the system:

$ free -lm
             total       used       free     shared    
Mem:          3008       2495        513         57

I know how to get the main memory consumption (RSS) of worker processes in Nginx:

$ ps -C nginx -O rss
  PID   RSS S TTY          TIME COMMAND
 1564  1336 S ?        00:00:00 nginx: master process /usr/sbin/nginx
 1565  1756 S ?        00:00:00 nginx: worker process
 1566  1756 S ?        00:00:00 nginx: worker process
 1567  1756 S ?        00:00:00 nginx: worker process
 1568  1756 S ?        00:00:00 nginx: worker process

Now to determine how many worker processes my system could use without resorting to swap:

echo $((3008 * 1024))
3080192
$ echo $((3080192 / 1756))
1754

My server can handle 1754 nginx workers without resorting to swap. However, it would be great if I can take this multi-step process above and make it a single line which can be executed from command line.

My problem is I don't know how to extract that "3008" from the free command from the command line. How can I tackle this?

1
  • See the tag text-processing on this site. Also see sed and awk. Commented Oct 8, 2016 at 18:52

4 Answers 4

1

My problem is I don't know how to extract that "3008" from the free command from the command line

Given this output:

free -lm
             total       used       free     shared    buffers     cached
Mem:          3757       1765       1991        138        122        766
Low:          3757       1765       1991
High:            0          0          0
-/+ buffers/cache:        876       2880
Swap:         7772          0       7772


Try this:

free -lm | grep '^Mem' | awk '{ print $2 }'
3757

Which will return the total column in the Mem: row. In my case 3757.

2
  • You never need to pipe grep into awk;  just do free -lm | awk '/^Mem/ { print $2 }'. Commented Oct 9, 2016 at 6:32
  • @G-Man Yes, you are absolutely right. Grep is not necessary in this case, thanks for pointing this out. Commented Oct 9, 2016 at 14:08
1

Piping free to awk will extract the value you require:

free -l | awk '/^Mem/{print $2}'

1
  • Note that omitting -m means the value is already in Kbytes and doesn't need multiplying. Commented Oct 9, 2016 at 0:28
0

Something like:

echo $(($(($(free -lm | grep Mem | awk '{print $2}') * 1024)) / $(ps -C nginx -O rss |grep 'nginx: worker process$' | awk '{print $2}' | tail -1)))

0

You should be able to do this with head, tail and cut:

free -lm | head -2 | tail -1 | tr -s ' ' | cut -f2 -d' '

head -2 because it is the top two lines.

             total       used       free     shared    buffers     cached
Mem:          3757       1765       1991        138        122        766

tail -1 because it is the last line of that.

Mem:          3757       1765       1991        138        122        766

tr -s ' ' - converts chains of whitespace to a single space.

Mem: 3757 1765 1991 138 122 766

Finally - cut -f2 -d' ' - getting the second field, space delimited.

3757

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.