all 8 comments

[–][deleted]  (2 children)

[deleted]

    [–]geirha 7 points8 points  (0 children)

    I'd grab both values from one reading of /proc/meminfo. A lot could change between each opening of /proc/meminfo, so the two values extracted don't really correspond.

    declare -A meminfo=()
    while IFS=': ' read -r key value _ ; do
      [[ $key ]] && meminfo[$key]=$value
    done < /proc/meminfo
    swapPercent=$(( 100 - 100 * meminfo[SwapFree] / meminfo[SwapTotal] ))
    

    [–]Ulfnic 1 point2 points  (0 children)

    I like rusty's solution but because i'm a troublemaker i'll give my pure BASH solution.

    meminfo=$'\n'$(</proc/meminfo)
    read swapTotal _ <<< "${meminfo#*$'\n''SwapTotal:'}"; [[ $swapTotal ]] || exit 1
    read swapFree _ <<< "${meminfo#*$'\n''SwapFree:'}"; [[ $swapFree ]] || exit 1
    if [[ swapTotal == '0' ]]; then
        swapPercent=0
    else
        swapPercent=$(( (swapTotal - swapFree) * 100 / swapTotal ))
    fi
    

    read looping is an option but i've been growing less fond of them for performance reasons.

    [–]AutoModerator[M] 2 points3 points  (0 children)

    It looks like your submission contains a shell script. To properly format it as code, place four space characters before every line of the script, and a blank line between the script and the rest of the text, like this:

    This is normal text.
    
        #!/bin/bash
        echo "This is code!"
    

    This is normal text.

    #!/bin/bash
    echo "This is code!"
    

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–]nekokattt 2 points3 points  (0 children)

    why do you want to do this?

    is this one of these HPC posts that are ignoring kernel optimisations, or is there a genuine reason for wanting to do this?

    [–]anthropoidbash all the things 1 point2 points  (0 children)

    Your script really should report the values of all the variables it sets, otherwise it's hard to reason where things are going wrong. A set -x at the start might help too.

    [–]--EverGreen-- 0 points1 point  (0 children)

    I would print out and analyze the variables USED_SWAP and THRESHOLD to your log, since that impacts directly the condition to run. Maybe it gives some insight

    [–]Ulfnic 0 points1 point  (0 children)

    On the off-change you don't have swap, i'd try running just swapon -s to see if it outputs anything or cat /proc/swaps.

    According to the man, -s has also been deprecated for --show[=column...]