diff --git a/genswap.sh b/genswap.sh index fb042ae..2734df3 100755 --- a/genswap.sh +++ b/genswap.sh @@ -4,7 +4,8 @@ SWAPPATH=/ SWAPNAME="swapfile" TOTALRAM=$(free -m | gawk '/Mem:/{print $2}') SWAPPINESS="10" -SYSCTLCONF="/etc/sysctl.conf" +SYSCTLCONF="sysctl.conf" +SYSCTLDIR="sysctl.conf.d" VERSION="0.1.0" BUILDDATE="2018-06-05" @@ -98,12 +99,24 @@ function autoSwap(){ } function swappiness(){ + + echo -e -n "\n${YELLOW}Enter a number for swappiness (0-100)[default=10]: ${NC}" read setSwappiness if [ -z "${setSwappiness}" ];then echo -e "\n${LCYAN}Setting default (10)\n${NC}" - echo "vm.swappiness=${SWAPPINESS}" | sudo tee -a /etc/sysctl.conf + + if [ -d "/etc/${SYSCTLDIR}" ];then + + SYSCTLCONF="swappiness.conf" + touch /etc/${SYSCTLDIR}/${SYSCTLCONF} + echo "vm.swappiness=${SWAPPINESS}" | sudo tee -a /etc/${SYSCTLDIR}/${SYSCTLCONF} + + else + + echo "vm.swappiness=${SWAPPINESS}" | sudo tee -a /etc/${SYSCTLCONF} + fi elif [[ ! "${setSwappiness}" =~ ^-?[0-9]+$ ]];then @@ -119,9 +132,30 @@ function swappiness(){ exit 0 else - echo "vm.swappiness=${setSwappiness}" | sudo tee -a ${SYSCTLCONF} - SWAPPINESSSET=$(cat ${SYSCTLCONF} | grep vm.swappiness=${setSwappiness}) - if [ ${SWAPPINESSSET} = "vm.swappiness=${setSwappiness}" ] ;then + if [ -d "/etc/${SYSCTLDIR}" ];then + + SYSCTLCONF="swappiness.conf" + USINGSYSCTLDIR="true" + touch /etc/${SYSCTLDIR}/${SYSCTLCONF} + echo "vm.swappiness=${setSwappiness}" > /etc/${SYSCTLDIR}/${SYSCTLCONF} + + else + + echo "vm.swappiness=${setSwappiness}" >> /etc/${SYSCTLCONF} + fi + + + if [ "${USINGSYSCTLDIR}" = "true" ];then + + SWAPPINESSSET=$(cat /etc/${SYSCTLDIR}/${SYSCTLCONF} | grep vm.swappiness=${setSwappiness}) + + else + + SWAPPINESSSET=$(cat /etc/${SYSCTLCONF} | grep vm.swappiness=${setSwappiness}) + + fi + + if [[ ${SWAPPINESSSET} = "vm.swappiness=${setSwappiness}" ]] ;then echo -e "${LGREEN}Swappiness value set successfully!\n${NC}" else echo -e "${LRED}Swappiness not set sucessfully...\n${NC}"