Gigi Duru
Gigi Duru

Reputation: 3

Automount swap script

Trying to create a script which is checking if swap is mounted with cat /proc/meminfo, if output is greather than 0, log a message: Swap already mounted, if not execute a command: swapon /swap

#!/bin/sh

if cat /proc/meminfo | grep SwapTotal | awk '{print $2}' = 0
then
swapon /swap
else
echo Swap already mounted
fi

Upvotes: 0

Views: 91

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133538

try:

if [[ -z $(awk '/SwapTotal/{print $2}' /proc/meminfo)  ]]
then
    swapon /swap
else
    echo Swap already mounted
fi

Upvotes: 0

VIPIN KUMAR
VIPIN KUMAR

Reputation: 3147

try this command -

awk '/SwapTotal/ {if($2==0) {system("swapon /swap")} else print "Swap already mounted"}' /proc/meminfo

Upvotes: 1

Related Questions