0

Swap2Ram.sh always produces its error message, even when I have enough ram left. Any ideas?

#!/bin/sh

mem=$(free  | awk '/Mem:/ {print $4}')
swap=$(free | awk '/Swap:/ {print $3}')

if [ $mem -lt $swap ]; then
    echo "ERROR: not enough RAM to write swap back, nothing done" >&2
    exit 1
fi

swapoff -a && 
swapon -a

I am on Linux 3.13.0-52-generic #86-Ubuntu SMP x86_64 x86_64 x86_64 GNU/Linux. Ubuntu Gnome.

Update $ LC_ALL=C free

             total       used       free     shared    buffers     cached
Mem:       3841288    3161152     680136     149232      42076     424216
-/+ buffers/cache:    2694860    1146428
Swap:      3983356      35300    3948056
  • How do you know you have enough RAM left? Post an output of free for which you get this result. – muru May 19 '15 at 11:59
  • Works for me. I'd suggest looking at the output of free (or adding a couple of echos) to see what's really going on. – Oli May 19 '15 at 12:06
  • @muru Gesamt Belegt(used) Frei(free) Gemeinsam Puffer Cache (RAM)Speicher: 3841288 3109452 731836 210320 41844 483996 -/+ Puffer/Cache: 2583612 1257676 (Swap)Auslagerungsdatei: 3983356 35300 3948056 – brubaker May 19 '15 at 12:21
  • @brubaker just run LC_ALL=C free and edit your post to add the output. It might also be a localisation issue - the command searches for Mem and Swap, which may change depending on the locale. – muru May 19 '15 at 12:23
  • @muru Damn! (I bet) your are right with the localisation issue. – brubaker May 19 '15 at 12:26
1

I think the problem is that the script looks for Mem and Swap, whereas your localization produces Speicher and Auslagerungsdatei. Just add an LC_ALL=C before the frees in the script:

export LC_ALL=C
mem=$(free  | awk '/Mem:/ {print $4}')
swap=$(free | awk '/Swap:/ {print $3}')

Or:

mem=$(LC_ALL=C free  | awk '/Mem:/ {print $4}')
swap=$(LC_ALL=C free | awk '/Swap:/ {print $3}')
  • This is the new now working script, based on your solution: #!/bin/sh mem=$(free | awk '/Speicher:/ {print $4}') swap=$(free | awk '/Auslagerungsdatei:/ {print $3}') if [ $mem -lt $swap ]; then echo "ERROR: not enough RAM to write swap back, nothing done" >&2 exit 1 fi swapoff -a && swapon -a – brubaker May 19 '15 at 12:30
  • @brubaker I see you changed the strings instead. Just be careful if you ever change your locale. – muru May 19 '15 at 12:31
  • I added "_de.sh" to the script name and did some comments inside. I edited the script seconds before your post. So I didn't saw your solution. :) But I am sure I got some bonus points for doing it myself. ;) – brubaker May 19 '15 at 12:34
  • @brubaker setting the locale to C, since the behaviour with C is well known - and it will work even if your preferred locale changes. – muru May 19 '15 at 12:36
  • I will change it like you proposed. TY again. – brubaker May 19 '15 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.