Skip to content

Instantly share code, notes, and snippets.

@IdoBar
Last active January 10, 2021 16:09
Show Gist options
  • Select an option

  • Save IdoBar/f3aff3d82c7245bf8157f46153eb86e6 to your computer and use it in GitHub Desktop.

Select an option

Save IdoBar/f3aff3d82c7245bf8157f46153eb86e6 to your computer and use it in GitHub Desktop.

Revisions

  1. IdoBar revised this gist Jan 10, 2021. No changes.
  2. IdoBar created this gist Jan 10, 2021.
    84 changes: 84 additions & 0 deletions qstat
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    #!/bin/bash
    # put this file in $HOME/bin and make it executable
    # make sure that this file is being called by default, I did this by adding `alias "qstat=~/bin/qstat"` to my ~/.bashrc

    # Servers
    #awoonga="@awongmgmr1.storage:16001 @awonmgr2.storage:16001"
    #flashlite="@flm1.ipoib:16001 @flashmgr2.ipoib:16001"
    #tinaroo="@tinmgmr1.ib0:16001 @tinmgr2.ib0:16001"
    awoonga="@awonmgr2"
    flashlite="@flashmgr2"
    tinaroo="@tinmgr2"

    opts="${*}"
    servers="${awoonga} ${flashlite} ${tinaroo}"

    TEMP=`getopt -o BD:EF:GHJLMQTafinqrstu:w1 --long version -n 'qstat' -- "$@"`
    eval set -- "$TEMP"
    # Need to find options that will allow/suppress the wide default
    while [ $# -gt 0 ]; do
    case "$1" in
    -B) opt_B=1; shift ;;
    -Q) opt_Q=1; shift ;;
    -L) opt_L=1 ; shift ;;
    -a) allow_w=1 ; shift ;;
    -n) allow_w=1 ; shift ;;
    -T) allow_w=1 ; shift ;;
    -s) allow_w=1 ; shift ;;
    -f) allow_w=1 ; opt_f=1; shift ;;
    -q) deny_w=1 ; shift ;;
    -h) deny_w=1 ; shift ;;
    -F) deny_w=1 ; shift ;;
    -D|-F|-u) shift 2 ;;
    @*) servers="" ; shift ;;
    *) shift ;;
    esac
    done

    # Discover the process tree that invoked us.
    ptree=$(/bin/ps -o'comm')

    defs=""; wide=""
    # If script or terminal sufficiently wide

    if [ ${allow_w:-0} -eq 1 ] && [ ${deny_w:-0} -eq 0 ]; then
    if ! /bin/tty -s || [ "$(/bin/tput cols)" -gt 136 ]; then wide="-w"; fi
    fi
    # this line makes sure that all servers are being queried when using qstat -f
    if [ ${opt_f:-0} -eq 1 ]; then
    /bin/timeout --foreground --kill-after=10s 10s /opt/pbs/bin/qstat ${defs} ${opts} ${wide} ${servers}
    else

    if [ "${opts#-L}" != "${opts}" ] || [ "${opts#-f}" != "${opts}" ]; then

    servers=""
    opts="${opts#-L}"; opts=${opts## }
    if [ "${opts:-x}" != "x" ] && [ "${opts#-}" == "${opts}" ]; then opts="-${opts}"; fi
    /opt/pbs/bin/qstat ${defs} ${opts} ${wide} ${server}
    # Query for all servers - but timeout on no response.

    elif [ ${opt_B:-0} -eq 1 ]; then
    # If this is "-B" remove the "@" from servernames
    shopt -s extglob
    if [ "${opts##-B*( )}" != "" ]; then servers=""; fi
    for server in ${servers//@}; do
    /bin/timeout --foreground --kill-after=10s 10s /opt/pbs/bin/qstat ${defs} ${opts} ${server}
    done
    elif [ ${opts_Q:-0} -eq 1 ]; then
    # If this is "-Q", need to add server headers
    for server in ${servers}; do
    sname=${server//@}; sname=${sname%:.*}
    /bin/echo -e "\n${sname}\n"
    /bin/timeout --foreground --kill-after=10s 10s /opt/pbs/bin/qstat -Q ${server}
    done
    else
    # If script or terminal sufficiently wide
    # Set default view
    if [ "${opts:-x}" == "x" ]; then defs="-1n"; fi
    for server in ${servers}; do
    /bin/timeout --foreground --kill-after=10s 10s /opt/pbs/bin/qstat ${defs} ${opts} ${wide} ${server}
    done
    fi
    fi
    # Delay return if invoked from "watch"
    if [ "${ptree}" != "${ptree/watch}" ]; then sleep 20; fi