bash how to check size of the swap
I have a bash script which checks if a swap exists or not and if it doesn't exists, then it creates one;
if free | awk '/^Swap:/ {exit !$2}'; then
echo "Have swap, skipping"
sudo swapoff -a
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo mkswap /swapfile
sudo swapon /swapfile
else
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
sudo echo -e "/swapfile none swap sw 0 0 n" >> /etc/fstab
fi
Now I also want to add this a check to find out the swap size - if it already exists - because if the swap already exists and it is 4GB then I will downgrade it to 2GB instead of skipping.
How can I do this?
Edit: The script currently creates a 2GB of swap file whether it exists or not so if I have 4GB swap file, it will change it to 2GB but if I also have 2GB swap file it will still re-do the swap with 2GB. I thought this is not a good option to go with so that's why I wonder if I should add a swap size check or not?
bash shell-script
|
show 4 more comments
I have a bash script which checks if a swap exists or not and if it doesn't exists, then it creates one;
if free | awk '/^Swap:/ {exit !$2}'; then
echo "Have swap, skipping"
sudo swapoff -a
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo mkswap /swapfile
sudo swapon /swapfile
else
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
sudo echo -e "/swapfile none swap sw 0 0 n" >> /etc/fstab
fi
Now I also want to add this a check to find out the swap size - if it already exists - because if the swap already exists and it is 4GB then I will downgrade it to 2GB instead of skipping.
How can I do this?
Edit: The script currently creates a 2GB of swap file whether it exists or not so if I have 4GB swap file, it will change it to 2GB but if I also have 2GB swap file it will still re-do the swap with 2GB. I thought this is not a good option to go with so that's why I wonder if I should add a swap size check or not?
bash shell-script
Why not always do aswapoff
andfallocate
followed byswapon
? While properly catching any failed command, of course.
– Kusalananda
Feb 22 at 9:34
I'm not sure what do you mean
– Marry Jane
Feb 22 at 9:41
I'm suggesting, and it's only a suggestion (because I don't know what your system is doing, and it may be inappropriate or impossible), that you always create a 2 Gb swap file, regardless of whether one existed before or what its previous size was.
– Kusalananda
Feb 22 at 9:49
Am I? I thought I was checking if whether a swap exists or not and then create one if it doesn't exists. See the firstif
condition in my code?
– Marry Jane
Feb 22 at 9:55
Re-read my comments. I'm never saying that you are or aren't doing something. I'm suggesting.
– Kusalananda
Feb 22 at 9:58
|
show 4 more comments
I have a bash script which checks if a swap exists or not and if it doesn't exists, then it creates one;
if free | awk '/^Swap:/ {exit !$2}'; then
echo "Have swap, skipping"
sudo swapoff -a
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo mkswap /swapfile
sudo swapon /swapfile
else
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
sudo echo -e "/swapfile none swap sw 0 0 n" >> /etc/fstab
fi
Now I also want to add this a check to find out the swap size - if it already exists - because if the swap already exists and it is 4GB then I will downgrade it to 2GB instead of skipping.
How can I do this?
Edit: The script currently creates a 2GB of swap file whether it exists or not so if I have 4GB swap file, it will change it to 2GB but if I also have 2GB swap file it will still re-do the swap with 2GB. I thought this is not a good option to go with so that's why I wonder if I should add a swap size check or not?
bash shell-script
I have a bash script which checks if a swap exists or not and if it doesn't exists, then it creates one;
if free | awk '/^Swap:/ {exit !$2}'; then
echo "Have swap, skipping"
sudo swapoff -a
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo mkswap /swapfile
sudo swapon /swapfile
else
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
sudo echo -e "/swapfile none swap sw 0 0 n" >> /etc/fstab
fi
Now I also want to add this a check to find out the swap size - if it already exists - because if the swap already exists and it is 4GB then I will downgrade it to 2GB instead of skipping.
How can I do this?
Edit: The script currently creates a 2GB of swap file whether it exists or not so if I have 4GB swap file, it will change it to 2GB but if I also have 2GB swap file it will still re-do the swap with 2GB. I thought this is not a good option to go with so that's why I wonder if I should add a swap size check or not?
bash shell-script
bash shell-script
edited Feb 22 at 10:07
Marry Jane
asked Feb 22 at 9:16
Marry JaneMarry Jane
104
104
Why not always do aswapoff
andfallocate
followed byswapon
? While properly catching any failed command, of course.
– Kusalananda
Feb 22 at 9:34
I'm not sure what do you mean
– Marry Jane
Feb 22 at 9:41
I'm suggesting, and it's only a suggestion (because I don't know what your system is doing, and it may be inappropriate or impossible), that you always create a 2 Gb swap file, regardless of whether one existed before or what its previous size was.
– Kusalananda
Feb 22 at 9:49
Am I? I thought I was checking if whether a swap exists or not and then create one if it doesn't exists. See the firstif
condition in my code?
– Marry Jane
Feb 22 at 9:55
Re-read my comments. I'm never saying that you are or aren't doing something. I'm suggesting.
– Kusalananda
Feb 22 at 9:58
|
show 4 more comments
Why not always do aswapoff
andfallocate
followed byswapon
? While properly catching any failed command, of course.
– Kusalananda
Feb 22 at 9:34
I'm not sure what do you mean
– Marry Jane
Feb 22 at 9:41
I'm suggesting, and it's only a suggestion (because I don't know what your system is doing, and it may be inappropriate or impossible), that you always create a 2 Gb swap file, regardless of whether one existed before or what its previous size was.
– Kusalananda
Feb 22 at 9:49
Am I? I thought I was checking if whether a swap exists or not and then create one if it doesn't exists. See the firstif
condition in my code?
– Marry Jane
Feb 22 at 9:55
Re-read my comments. I'm never saying that you are or aren't doing something. I'm suggesting.
– Kusalananda
Feb 22 at 9:58
Why not always do a
swapoff
and fallocate
followed by swapon
? While properly catching any failed command, of course.– Kusalananda
Feb 22 at 9:34
Why not always do a
swapoff
and fallocate
followed by swapon
? While properly catching any failed command, of course.– Kusalananda
Feb 22 at 9:34
I'm not sure what do you mean
– Marry Jane
Feb 22 at 9:41
I'm not sure what do you mean
– Marry Jane
Feb 22 at 9:41
I'm suggesting, and it's only a suggestion (because I don't know what your system is doing, and it may be inappropriate or impossible), that you always create a 2 Gb swap file, regardless of whether one existed before or what its previous size was.
– Kusalananda
Feb 22 at 9:49
I'm suggesting, and it's only a suggestion (because I don't know what your system is doing, and it may be inappropriate or impossible), that you always create a 2 Gb swap file, regardless of whether one existed before or what its previous size was.
– Kusalananda
Feb 22 at 9:49
Am I? I thought I was checking if whether a swap exists or not and then create one if it doesn't exists. See the first
if
condition in my code?– Marry Jane
Feb 22 at 9:55
Am I? I thought I was checking if whether a swap exists or not and then create one if it doesn't exists. See the first
if
condition in my code?– Marry Jane
Feb 22 at 9:55
Re-read my comments. I'm never saying that you are or aren't doing something. I'm suggesting.
– Kusalananda
Feb 22 at 9:58
Re-read my comments. I'm never saying that you are or aren't doing something. I'm suggesting.
– Kusalananda
Feb 22 at 9:58
|
show 4 more comments
1 Answer
1
active
oldest
votes
Assuming your kernel is not really old and supports the fallocate
system call (available since version 2.6.23, see man fallocate(1)
and man fallocate(2)
), fallocate
will likely be fast, since it does not write data blocks. Hence, there is no big issue with always creating a new swap file. The only step you will likely want to perform conditionally is editing your fstab
.
Assuming you either have no swap at all or have exactly one swap file whose path is /swapfile
:
swapfile="/swapfile"
# Make sure swap is on
swapon --all
# Check if our assumptions hold
if [ "$(swapon --show --noheadings | wc -l)" -gt 1 ] ||
( [ "$(swapon --show --noheadings | wc -l)" -eq 1 ] &&
( [ "$(swapon --show=TYPE --noheadings)" != 'file' ] ||
[ "$(swapon --show=NAME --noheadings)" != "$swapfile" ]
)
); then
echo "Unsafe to proceed, exiting."
exit
fi
# Edit /etc/fstab if our file is not already there
if ! grep -q '^[[:blank:]]*'"$swapfile"'[[:blank:]]{1,}none[[:blank:]]{1,}swap[[:blank:]]{1,}' /etc/fstab;
then
printf '%sn' "$swapfile none swap sw 0 0" >> /etc/fstab
fi
# Create/replace the swap file
swapoff --all
[ -f "$swapfile" ] && rm -- "$swapfile"
fallocate -l 2GiB -- "$swapfile"
chmod 600 "$swapfile"
mkswap -- "$swapfile"
swapon --all
You may still want to avoid unnecessarily turning off and on your swap: it may be a slow operation if a substantial fraction of it is used and it can have unwanted consequences if not enough memory is available.
To partially address these concerns (and also answer your original question), the last part of the above code can be enclosed in a conditional block:
# Check if we want to shrink the swap file i.e. it is bigger than 2 GiB
# (or if we have no swap file)
if [ "$(free | awk '/^Swap:/ { print $2 }')" = "0" ] ||
[ "$(free --bytes | awk '/^Swap:/ { print $2 }')" -gt 2147483648 ]; then
# Create/replace the swap file
# Same as above...
fi
Finally, assuming your kernel version is at least 3.14 and provides MemAvailable
in /proc/meminfo
(whose value is reported as the available
column by free
, see man free(1)
), you can also check if you have enough available memory before trying to turn off swap.
The last code snippet becomes:
# Do we have no swap or more swap than 2GB?
# If yes, do we have more available memory than used swap?
if ( [ "$(free | awk '/^Swap:/ { print $2 }')" = "0" ] ||
[ "$(free --bytes | awk '/^Swap:/ { print $2 }')" -gt 2147483648 ]
) &&
[ "$(awk '/MemAvailable:/ { print $2 }' /proc/meminfo)" -gt "$(free | awk '/Swap:/ { print $3 }')" ];
then
# Create/replace the swap file
# Same as above...
fi
Working like a charm now! Thanks a bunch!
– Marry Jane
Feb 22 at 16:11
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502258%2fbash-how-to-check-size-of-the-swap%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming your kernel is not really old and supports the fallocate
system call (available since version 2.6.23, see man fallocate(1)
and man fallocate(2)
), fallocate
will likely be fast, since it does not write data blocks. Hence, there is no big issue with always creating a new swap file. The only step you will likely want to perform conditionally is editing your fstab
.
Assuming you either have no swap at all or have exactly one swap file whose path is /swapfile
:
swapfile="/swapfile"
# Make sure swap is on
swapon --all
# Check if our assumptions hold
if [ "$(swapon --show --noheadings | wc -l)" -gt 1 ] ||
( [ "$(swapon --show --noheadings | wc -l)" -eq 1 ] &&
( [ "$(swapon --show=TYPE --noheadings)" != 'file' ] ||
[ "$(swapon --show=NAME --noheadings)" != "$swapfile" ]
)
); then
echo "Unsafe to proceed, exiting."
exit
fi
# Edit /etc/fstab if our file is not already there
if ! grep -q '^[[:blank:]]*'"$swapfile"'[[:blank:]]{1,}none[[:blank:]]{1,}swap[[:blank:]]{1,}' /etc/fstab;
then
printf '%sn' "$swapfile none swap sw 0 0" >> /etc/fstab
fi
# Create/replace the swap file
swapoff --all
[ -f "$swapfile" ] && rm -- "$swapfile"
fallocate -l 2GiB -- "$swapfile"
chmod 600 "$swapfile"
mkswap -- "$swapfile"
swapon --all
You may still want to avoid unnecessarily turning off and on your swap: it may be a slow operation if a substantial fraction of it is used and it can have unwanted consequences if not enough memory is available.
To partially address these concerns (and also answer your original question), the last part of the above code can be enclosed in a conditional block:
# Check if we want to shrink the swap file i.e. it is bigger than 2 GiB
# (or if we have no swap file)
if [ "$(free | awk '/^Swap:/ { print $2 }')" = "0" ] ||
[ "$(free --bytes | awk '/^Swap:/ { print $2 }')" -gt 2147483648 ]; then
# Create/replace the swap file
# Same as above...
fi
Finally, assuming your kernel version is at least 3.14 and provides MemAvailable
in /proc/meminfo
(whose value is reported as the available
column by free
, see man free(1)
), you can also check if you have enough available memory before trying to turn off swap.
The last code snippet becomes:
# Do we have no swap or more swap than 2GB?
# If yes, do we have more available memory than used swap?
if ( [ "$(free | awk '/^Swap:/ { print $2 }')" = "0" ] ||
[ "$(free --bytes | awk '/^Swap:/ { print $2 }')" -gt 2147483648 ]
) &&
[ "$(awk '/MemAvailable:/ { print $2 }' /proc/meminfo)" -gt "$(free | awk '/Swap:/ { print $3 }')" ];
then
# Create/replace the swap file
# Same as above...
fi
Working like a charm now! Thanks a bunch!
– Marry Jane
Feb 22 at 16:11
add a comment |
Assuming your kernel is not really old and supports the fallocate
system call (available since version 2.6.23, see man fallocate(1)
and man fallocate(2)
), fallocate
will likely be fast, since it does not write data blocks. Hence, there is no big issue with always creating a new swap file. The only step you will likely want to perform conditionally is editing your fstab
.
Assuming you either have no swap at all or have exactly one swap file whose path is /swapfile
:
swapfile="/swapfile"
# Make sure swap is on
swapon --all
# Check if our assumptions hold
if [ "$(swapon --show --noheadings | wc -l)" -gt 1 ] ||
( [ "$(swapon --show --noheadings | wc -l)" -eq 1 ] &&
( [ "$(swapon --show=TYPE --noheadings)" != 'file' ] ||
[ "$(swapon --show=NAME --noheadings)" != "$swapfile" ]
)
); then
echo "Unsafe to proceed, exiting."
exit
fi
# Edit /etc/fstab if our file is not already there
if ! grep -q '^[[:blank:]]*'"$swapfile"'[[:blank:]]{1,}none[[:blank:]]{1,}swap[[:blank:]]{1,}' /etc/fstab;
then
printf '%sn' "$swapfile none swap sw 0 0" >> /etc/fstab
fi
# Create/replace the swap file
swapoff --all
[ -f "$swapfile" ] && rm -- "$swapfile"
fallocate -l 2GiB -- "$swapfile"
chmod 600 "$swapfile"
mkswap -- "$swapfile"
swapon --all
You may still want to avoid unnecessarily turning off and on your swap: it may be a slow operation if a substantial fraction of it is used and it can have unwanted consequences if not enough memory is available.
To partially address these concerns (and also answer your original question), the last part of the above code can be enclosed in a conditional block:
# Check if we want to shrink the swap file i.e. it is bigger than 2 GiB
# (or if we have no swap file)
if [ "$(free | awk '/^Swap:/ { print $2 }')" = "0" ] ||
[ "$(free --bytes | awk '/^Swap:/ { print $2 }')" -gt 2147483648 ]; then
# Create/replace the swap file
# Same as above...
fi
Finally, assuming your kernel version is at least 3.14 and provides MemAvailable
in /proc/meminfo
(whose value is reported as the available
column by free
, see man free(1)
), you can also check if you have enough available memory before trying to turn off swap.
The last code snippet becomes:
# Do we have no swap or more swap than 2GB?
# If yes, do we have more available memory than used swap?
if ( [ "$(free | awk '/^Swap:/ { print $2 }')" = "0" ] ||
[ "$(free --bytes | awk '/^Swap:/ { print $2 }')" -gt 2147483648 ]
) &&
[ "$(awk '/MemAvailable:/ { print $2 }' /proc/meminfo)" -gt "$(free | awk '/Swap:/ { print $3 }')" ];
then
# Create/replace the swap file
# Same as above...
fi
Working like a charm now! Thanks a bunch!
– Marry Jane
Feb 22 at 16:11
add a comment |
Assuming your kernel is not really old and supports the fallocate
system call (available since version 2.6.23, see man fallocate(1)
and man fallocate(2)
), fallocate
will likely be fast, since it does not write data blocks. Hence, there is no big issue with always creating a new swap file. The only step you will likely want to perform conditionally is editing your fstab
.
Assuming you either have no swap at all or have exactly one swap file whose path is /swapfile
:
swapfile="/swapfile"
# Make sure swap is on
swapon --all
# Check if our assumptions hold
if [ "$(swapon --show --noheadings | wc -l)" -gt 1 ] ||
( [ "$(swapon --show --noheadings | wc -l)" -eq 1 ] &&
( [ "$(swapon --show=TYPE --noheadings)" != 'file' ] ||
[ "$(swapon --show=NAME --noheadings)" != "$swapfile" ]
)
); then
echo "Unsafe to proceed, exiting."
exit
fi
# Edit /etc/fstab if our file is not already there
if ! grep -q '^[[:blank:]]*'"$swapfile"'[[:blank:]]{1,}none[[:blank:]]{1,}swap[[:blank:]]{1,}' /etc/fstab;
then
printf '%sn' "$swapfile none swap sw 0 0" >> /etc/fstab
fi
# Create/replace the swap file
swapoff --all
[ -f "$swapfile" ] && rm -- "$swapfile"
fallocate -l 2GiB -- "$swapfile"
chmod 600 "$swapfile"
mkswap -- "$swapfile"
swapon --all
You may still want to avoid unnecessarily turning off and on your swap: it may be a slow operation if a substantial fraction of it is used and it can have unwanted consequences if not enough memory is available.
To partially address these concerns (and also answer your original question), the last part of the above code can be enclosed in a conditional block:
# Check if we want to shrink the swap file i.e. it is bigger than 2 GiB
# (or if we have no swap file)
if [ "$(free | awk '/^Swap:/ { print $2 }')" = "0" ] ||
[ "$(free --bytes | awk '/^Swap:/ { print $2 }')" -gt 2147483648 ]; then
# Create/replace the swap file
# Same as above...
fi
Finally, assuming your kernel version is at least 3.14 and provides MemAvailable
in /proc/meminfo
(whose value is reported as the available
column by free
, see man free(1)
), you can also check if you have enough available memory before trying to turn off swap.
The last code snippet becomes:
# Do we have no swap or more swap than 2GB?
# If yes, do we have more available memory than used swap?
if ( [ "$(free | awk '/^Swap:/ { print $2 }')" = "0" ] ||
[ "$(free --bytes | awk '/^Swap:/ { print $2 }')" -gt 2147483648 ]
) &&
[ "$(awk '/MemAvailable:/ { print $2 }' /proc/meminfo)" -gt "$(free | awk '/Swap:/ { print $3 }')" ];
then
# Create/replace the swap file
# Same as above...
fi
Assuming your kernel is not really old and supports the fallocate
system call (available since version 2.6.23, see man fallocate(1)
and man fallocate(2)
), fallocate
will likely be fast, since it does not write data blocks. Hence, there is no big issue with always creating a new swap file. The only step you will likely want to perform conditionally is editing your fstab
.
Assuming you either have no swap at all or have exactly one swap file whose path is /swapfile
:
swapfile="/swapfile"
# Make sure swap is on
swapon --all
# Check if our assumptions hold
if [ "$(swapon --show --noheadings | wc -l)" -gt 1 ] ||
( [ "$(swapon --show --noheadings | wc -l)" -eq 1 ] &&
( [ "$(swapon --show=TYPE --noheadings)" != 'file' ] ||
[ "$(swapon --show=NAME --noheadings)" != "$swapfile" ]
)
); then
echo "Unsafe to proceed, exiting."
exit
fi
# Edit /etc/fstab if our file is not already there
if ! grep -q '^[[:blank:]]*'"$swapfile"'[[:blank:]]{1,}none[[:blank:]]{1,}swap[[:blank:]]{1,}' /etc/fstab;
then
printf '%sn' "$swapfile none swap sw 0 0" >> /etc/fstab
fi
# Create/replace the swap file
swapoff --all
[ -f "$swapfile" ] && rm -- "$swapfile"
fallocate -l 2GiB -- "$swapfile"
chmod 600 "$swapfile"
mkswap -- "$swapfile"
swapon --all
You may still want to avoid unnecessarily turning off and on your swap: it may be a slow operation if a substantial fraction of it is used and it can have unwanted consequences if not enough memory is available.
To partially address these concerns (and also answer your original question), the last part of the above code can be enclosed in a conditional block:
# Check if we want to shrink the swap file i.e. it is bigger than 2 GiB
# (or if we have no swap file)
if [ "$(free | awk '/^Swap:/ { print $2 }')" = "0" ] ||
[ "$(free --bytes | awk '/^Swap:/ { print $2 }')" -gt 2147483648 ]; then
# Create/replace the swap file
# Same as above...
fi
Finally, assuming your kernel version is at least 3.14 and provides MemAvailable
in /proc/meminfo
(whose value is reported as the available
column by free
, see man free(1)
), you can also check if you have enough available memory before trying to turn off swap.
The last code snippet becomes:
# Do we have no swap or more swap than 2GB?
# If yes, do we have more available memory than used swap?
if ( [ "$(free | awk '/^Swap:/ { print $2 }')" = "0" ] ||
[ "$(free --bytes | awk '/^Swap:/ { print $2 }')" -gt 2147483648 ]
) &&
[ "$(awk '/MemAvailable:/ { print $2 }' /proc/meminfo)" -gt "$(free | awk '/Swap:/ { print $3 }')" ];
then
# Create/replace the swap file
# Same as above...
fi
edited Mar 3 at 10:56
answered Feb 22 at 12:19
fra-sanfra-san
1,8531519
1,8531519
Working like a charm now! Thanks a bunch!
– Marry Jane
Feb 22 at 16:11
add a comment |
Working like a charm now! Thanks a bunch!
– Marry Jane
Feb 22 at 16:11
Working like a charm now! Thanks a bunch!
– Marry Jane
Feb 22 at 16:11
Working like a charm now! Thanks a bunch!
– Marry Jane
Feb 22 at 16:11
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502258%2fbash-how-to-check-size-of-the-swap%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why not always do a
swapoff
andfallocate
followed byswapon
? While properly catching any failed command, of course.– Kusalananda
Feb 22 at 9:34
I'm not sure what do you mean
– Marry Jane
Feb 22 at 9:41
I'm suggesting, and it's only a suggestion (because I don't know what your system is doing, and it may be inappropriate or impossible), that you always create a 2 Gb swap file, regardless of whether one existed before or what its previous size was.
– Kusalananda
Feb 22 at 9:49
Am I? I thought I was checking if whether a swap exists or not and then create one if it doesn't exists. See the first
if
condition in my code?– Marry Jane
Feb 22 at 9:55
Re-read my comments. I'm never saying that you are or aren't doing something. I'm suggesting.
– Kusalananda
Feb 22 at 9:58