Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Bash script command to check swap and show error?

Sun Feb 17, 2019 5:31 pm

I have a script I use on new RPi units to install some software tools from source.
It works really well provided that the swap is set to 1GB but I tend to forget this and if it is the standard 100M then the scripted build winds into a problem some 30 minutes later. The error message does not say it is missing swap but that is the core reason nevertheless.

So I want to complement my script at the start with a swap test followed by and error message and exit if swap is set to less than 1000 MB.

I have looked at several commands to show the swap size on screen but I don't know how to extract the actual number and then compare it to my limit and make the script branch away when it is not OK.

Any suggestions on how to create such a function?

Here are some commands I have tested to find swap size:

Code: Select all

$ cat /proc/swaps
Filename                                Type            Size    Used    Priority
/var/swap                               file            102396  9984    -2

$ swapon -s
Filename                                Type            Size    Used    Priority
/var/swap                               file            102396  9984    -2

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           927M        125M         49M         12M        752M        725M
Swap:           99M        9.8M         90M

$ free
              total        used        free      shared  buff/cache   available
Mem:         949448      129776       48928       12736      770744      741436
Swap:        102396        9984       92412

$ free | grep Swap
Swap:        102396        9984       92412
The last command seems to extract the wanted value as the first numerical in one line, but how do I check the value against my limit?
I want to proceed only if the value is > 1000000.
Bo Berglund
Sweden

tpyo kingg
Posts: 809
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Bash script command to check swap and show error?

Sun Feb 17, 2019 5:41 pm

You could use AWK:

Code: Select all

free | awk '$1~/Mem/&&$2>1000000{print $2; exit 1}'; 
echo $?;
That'll work within an if-then-else shell statement.

jahboater
Posts: 5759
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Bash script command to check swap and show error?

Sun Feb 17, 2019 5:42 pm

I do this in my GCC build script that requires extra swap on the Pi:

Code: Select all

if [ -f /etc/dphys-swapfile ]; then
  sudo sed -i 's/^CONF_SWAPSIZE=[0-9]*$/CONF_SWAPSIZE=1024/' /etc/dphys-swapfile
  sudo /etc/init.d/dphys-swapfile restart
fi
Its a little crude in that it sets the swap size to 1GB regardless of what it was before.
There is no warning, it just quietly corrects it.

Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Re: Bash script command to check swap and show error?

Sun Feb 17, 2019 8:13 pm

tpyo kingg wrote:
Sun Feb 17, 2019 5:41 pm
You could use AWK:

Code: Select all

free | awk '$1~/Mem/&&$2>1000000{print $2; exit 1}'; 
echo $?;
That'll work within an if-then-else shell statement.
I made some tests with this by putting it into a test.sh file.
After changing Mem to Swap and 1000000 to 100000 the result I got was two lines:
102396 (this is the size of my swap file)
1 (what is this?)

I tried changing the print $2 into printing a text message but nothing was displayed then.

So what I don't understand is how to use this to make an if-then-else construct in the script like this pseudocode:

Code: Select all

SWAPSIZE=(function to retrieve swap size)
if $SWAPSIZE < 1000000 then
  echo "You need to expand swap space! 1000M is needed!";
  exit 1;
fi
If I can just get swap size into a variable then I could make a conditional.
Needless to say I am not good at scripting and I don't know awk either.
Bo Berglund
Sweden

epoch1970
Posts: 5132
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Bash script command to check swap and show error?

Sun Feb 17, 2019 9:15 pm

Shell built-in math operators work in integer, so something like $(( $grepped_value / 1000000)) should return 0 unless you’re over the 1M mark.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Re: Bash script command to check swap and show error?

Sun Feb 17, 2019 10:50 pm

Through googling I found that grep is capable of extracting data too.
So this is the original command to get the Swap data:

Code: Select all

$ free | grep Swap
Swap:        102396        9984       92412
If I feed the response string into another grep I get this:

Code: Select all

$ free | grep Swap | grep -P -o "[0-9]+" | head -1
102396
So this is what I want to put inside the script. When I do this it seems work...
Here is the test script:

Code: Select all

#!/bin/bash
SWAPSPACE=$(free | grep Swap | grep  -P -o "[0-9]+" | head -1)
echo $SWAPSPACE
And here is the result when I run it:

Code: Select all

$ ./test.sh
102396
But how can I use this? Well, I tested with this script:

Code: Select all

#!/bin/bash
SWAPSPACE=$(free | grep Swap | grep  -P -o "[0-9]+" | head -1)
if [ "$SWAPSPACE" -lt 1000000 ]; then
  echo "Swap space $SWAPSPACE too small! You must set 1 GB swap!"
  exit 1
fi
echo "Swap space $SWAPSPACE OK!"
and when I run it I get:

Code: Select all

 $ ./test.sh
Swap space 102396 too small! You must set 1 GB swap!
So it seems now OK!
Bo Berglund
Sweden

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Bash script command to check swap and show error?

Mon Feb 18, 2019 7:40 am

How about this method, which uses one command instead of chaining four together?

Code: Select all

SWAPSPACE=$(sed -n "s/^SwapTotal: *\([0-9]*\) kB/\1/p" /proc/meminfo)"
(I know some people here don't like using sed, oh well, it works).
Unreadable squiggle

Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Re: Bash script command to check swap and show error?

Mon Feb 18, 2019 7:32 pm

Yes sed is not what I am fluent in..
And in this case you would also have to know the file that holds swap info as well as the way to find the data item...
free as a general command seems a bit more in line with what I am comfortable with as well as the grep, head and tail commands.
New to me this time is the number of parameters one can give to grep...
Bo Berglund
Sweden

tpyo kingg
Posts: 809
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Bash script command to check swap and show error?

Mon Feb 18, 2019 7:43 pm

If you are combining grep with head or tail a lot you might look at awk instead on many of those occasions.

Code: Select all

SWAPSPACE=$(free | awk '$1=="Swap:"{print $2;exit}')
The above will capture the first and maybe only swap. If you go with grep the -P option you've found is very useful even if experimental. I got by for ages using just perl instead of sed or awk. For perl, the -e and -n or -p options are useful. If you take a look at awk, the -F option is often useful. It's worth learning a few basics of awk to find out what it can do quickly and easily.

grep < sed < awk < perl

Return to “Raspberry Pi OS”