I am wanting to write the amount of ram used to a file in a bash script.
if you run the command free you get the following output
total used free shared buffers cached
Mem: 7930 4103 3826 0 59 2060
-/+ buffers/cache: 1983 5946
Swap: 15487 0 15487
I am wanting to pull the used bit out and write to a file something like
MemUsed: 4103
I have tried varies of
cat free | grep used' uniq >> ramInfo.txt but have been unable to get it correct.
I am completely new to shell scripts so forgive me if this is relatively simple.
You can do this and you will get the value:
free -h | awk '/^Mem/{print $4}'
You can also get the memory free in Kilobytes from /proc/meminfo:
cat /proc/meminfo | awk -F':' '/MemFree/{print $2}' | sed 's/^ *//g;s/ *$//g'
Related
I have a following question from this question. Is the value of the total physical memory always shown in KB? Because I would like to print it in GB and I use this command
grep MemTotal /proc/meminfo | awk '{$2=$2/(1024^2); print $2}'
I'm not sure wheter I should add a if statement to prove the command grep MemTotal /proc/meminfo showing KB value or other value
Any help would be appreciated
You need not to use grep + awk, you could do this in a single awk itself. From explanation point of view, I have combined your attempted grep code within awk code itself. In awk program I am checking condition if 1st field is MemTotal: and 3rd field is kB then printing 2rd field's value in GB(taken from OP's attempted code itself).
awk '$1=="MemTotal:" && $3=="kB"{print $2/(1024^2)}' /proc/meminfo
OR if in case you want to make kB match in 3rd a case in-sensitive one then try following code:
awk '$1=="MemTotal:" && $3~/^[kK][bB]$/{print $2/(1024^2)}' /proc/meminfo
Is the value of the total physical memory always shown in KB?
Yes, the unit kB is fixed in the kernel code. See: 1 and 2
If you assume the MemTotal: entry is always the first line of /proc/meminfo, it is possible to get the Gigabyte value without spawning a sub-shell or external commands, and using only POSIX-shell grammar that works with ksh, ash, dsh, zsh, or bash:
#!/usr/bin/env sh
IFS=': ' read -r _ memTotal _ < /proc/meminfo;
printf 'Total RAM: %d GB\n' "$((memTotal / 1024000))"
I have this situation.
In my script, I have to use the hdparm command on specific partion and extract the MB/s value calculated.
I'm able to achieve this thanks the us of grep and regex; so, if with
sudo hdparm -tT /dev/xvda1
the output is:
/dev/xvda1:
Timing cached reads: 12596 MB in 1.99 seconds = 6320.55 MB/sec
Timing buffered disk reads: 594 MB in 3.01 seconds = 197.12 MB/sec
with
sudo hdparm -tT /dev/xvda1 | grep -Po '.* \K[0-9.]+'
the results are:
6320.55
197.12
Now, the next request is to print data in a different way.
The desired output is:
/dev/xvda1: 6320.55 MB/sec, 197.12 MB/sec
But I don't know how to obtain this; summarizing, what is requested is to print the partion and, in a single line, the MB/s values extracted.
Seems like your last question was an XY problem.
If you want to append MB/sec anyways there is no need to remove it in the first place. Extracting 6320.55 MB/sec would have been a lot easier than extracting just 6320.55.
Anyways, an awk script is probably the best solution here:
awk -F' = ' '{a[NR]=$NF} END {printf "%s %s, %s\n", a[1], a[2], a[3]}'
If you don't need exactly that format, the script can be simplified to:
awk -F' = ' '{printf "%s ", $NF}'
which prints /dev/xvda1: 6320.55 MB/sec 197.12 MB/sec .
Im creating a script which compare a size of a folder /home/user/files with a free space in /backups to ensure the copy can be done.
These returns size in (blocks?) and route
du -B 1 /home/user/files | cut -f 1 -d " "
12288 /home/user/files
But this returns in GB:
df -h /var | tail -1 | awk '{print $4}'
3.9G
And cant verifi with an if.
How can i get the same type of measure (blocks, kb...) and compare it to ensure free space is also to make a copy?
Thx!
Is there a command which outputs just current CPU usage percentage and current memory usage percentage? As a single number, so no tables or formatted output.
The reason I'm asking. For my panel in XFCE I'd like to see something like this:
CPU 34% | MEM 56%
I haven't found a plugin which does that, so I aim to use the Generic Monitor plugin and give it a command which it should print and let it update every 1 sec.
Put the following snippet somewhere in a script:
#!/bin/bash
CPU=$(lscpu | grep '\(CPU\|max\) MHz:' | xargs echo | awk '{printf "%3.0f\n", $3*100/$7}')
MEM=$(free | grep Mem | awk '{printf "%3.0f\n", $3*100/$2}')
echo CPU $CPU% \| MEM $MEM%
And call it from genmon as bash /path/to/this/script.sh.
Can somebody please help me to write a KSH Script to get the CPU usage of the AIX server ?
Here I want my script to get the Current usage of CPU that time it is executed
There are a number of tools on AIX (and elsewhere) to get the current CPU usage.
nmon
On AIX (and Linux) you have nmon. This gives very detailed infos on memory, cpu usage, disk usage, etc. It is normally used as an interactive tool.
sar
call sar -u 1 1 to get the current cpu usage. See the manual page of sar for a whole lot of options. Depending on your installation you need to be root or add your user to the group "adm".
Just call w -u. It outputs a little bit more than you ask for. If you don't need that you can use awk/sed/cut to cut it away.
I use the following script in bash, but I just tried it in ksh and it works all the same:
top -bn2 | grep 'Cpu(s)' | sed -n '2s/.*, *\([0-9.]*\)%* id.*/\1/p' | awk '{print "CPU: " 100 - $1" %"}
You can also use
top -bn1 | grep 'Cpu(s)' | sed -n 's/.*, *\([0-9.]*\)%* id.*/\1/p' | awk '{print "CPU: " 100 - $1" %"}'
for faster response, but the result will be less accurate.