• R/O
  • SSH

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

Revisãoa50ec831988ad8e62bcfa6dbe861534b3bc5638f (tree)
Hora2015-04-20 04:47:09
AutorLorenzo Isella <lorenzo.isella@gmai...>
CommiterLorenzo Isella

Mensagem de Log

A script to clear and reactivate the swap only when there is free memory available.

Mudança Sumário

Diff

diff -r bb13d36b2bd3 -r a50ec831988a Bash-scripts/swap_management.sh
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Bash-scripts/swap_management.sh Sun Apr 19 21:47:09 2015 +0200
@@ -0,0 +1,32 @@
1+#!/bin/bash
2+
3+# Make sure that all text is parsed in the same language
4+# export LC_MESSAGES=en_US.UTF-8
5+# export LC_COLLATE=en_US.UTF-8
6+# export LANG=en_US.utf8
7+# export LANGUAGE=en_US:en
8+# export LC_CTYPE=en_US.UTF-8
9+
10+# Calculate how much memory and swap is free
11+free_data="$(free)"
12+mem_data="$(echo "$free_data" | grep 'Mem:')"
13+free_mem="$(echo "$mem_data" | awk '{print $4}')"
14+buffers="$(echo "$mem_data" | awk '{print $6}')"
15+cache="$(echo "$mem_data" | awk '{print $7}')"
16+total_free=$((free_mem + buffers + cache))
17+used_swap="$(echo "$free_data" | grep 'Swap:' | awk '{print $3}')"
18+
19+echo -e "Free memory:\t$total_free kB ($((total_free / 1024)) MB)\nUsed swap:\t$used_swap kB ($((used_swap / 1024)) MB)"
20+
21+# Do the work
22+if [[ $used_swap -eq 0 ]]; then
23+ echo "Congratulations! No swap is in use."
24+elif [[ $used_swap -lt $total_free ]]; then
25+ echo "Freeing swap..."
26+ swapoff -a
27+ swapon -a
28+else
29+ echo "Not enough free memory. Exiting."
30+ exit 1
31+fi
32+