Zsh

From HPC Wiki
Jump to: navigation, search

The zsh offers a wide variety of possibile features to simplify command-line usage. If you're interested in a more user-friendly shell, look at zsh with the plugin ohmyzsh. It is easy to install and offers a wide variety of usability improvements, like better tab completion, integrated git status messages and so on.

If you're interested in one of these, put them in your ~/.zshrc.

Here are some useful functions for zsh, specific to Slurm-HPC-systems.


Auto-completion for scancel

function _scancel {
    SQUEUE_OUTPUT=$(squeue -o "%i:%j" -u $USER | grep -v "JOBID:NAME")
    SCANCEL_COMMANDS=(
        '--signal=:Signal type (USR1, USR2, INT etc.)'
        '--batch:Send signal to all batch steps'
    )
    
    while IFS= read -r line; do
        if [[ ! -z $line ]]; then
            SCANCEL_COMMANDS+=("$line")
        fi
    done <<< "$SQUEUE_OUTPUT"
    SCANCEL_COMMANDS_STR=$(printf "\n'%s'" "${SCANCEL_COMMANDS[@]}")
    eval "_describe 'command' \"($SCANCEL_COMMANDS_STR)\""
}

compdef _scancel "scancel"


Auto completion with lmod/ml

function _ml {
    ML_COMMANDS=(
        '-t:Show computer parsable output'
        'unload:Unload a Module'
        'spider:Search for a module'
        'avail:Show available modules'
        'list:List loaded modules'
    )
    ML_COMMANDS_STR=$(printf "\n'%s'" "${ML_COMMANDS[@]}")
    eval "_describe 'command' \"($ML_COMMANDS_STR)\""
    _values -s ' ' 'flags' $(ml -t avail | sed -e 's#/$##' | tr '\n' ' ')
}
compdef _ml "ml"


ftails

When using this, you will be able to follow jobs more easily. If you have just one sbatch job running, and enter ftails, it will automatically follow that one job. If you have more than one running, it will prompt you (via whiptail) which job to choose. It also adds slurmlogpath, which, when called like slurmlogpath $SBATCH_ID will print the log path to stdout.

function slurmlogpath { scontrol show job $1 | grep StdOut | sed -e 's/^\s*StdOut=//' }
function ftails { 
    JOBID=$1
    if [[ -z $JOBID ]]; then
        JOBS=$(squeue --format="%i \\'%j\\' " -u $USER | grep -v JOBID)
        NUMBER_OF_JOBS=$(echo "$JOBS" | wc -l)
        JOBID=
        if [[ "$NUMBER_OF_JOBS" -eq 1 ]]; then
            JOBID=$(echo $JOBS | sed -e "s/'//g" | sed -e 's/ .*//')
        else
            JOBS=$(echo $JOBS | tr -d '\n')

            JOBID=$(eval "whiptail --title 'Choose jobs to tail' --menu 'Choose Job to tail' 25 78 16 $JOBS" 3>&1 1>&2 2>&3)
        fi
    fi
    SLURMLOGPATH=$(slurmlogpath $JOBID)
    if [[ -e $SLURMLOGPATH ]]; then
        tail -n100 -f $SLURMLOGPATH
    else
        echo "No slurm-log-file found"
    fi
}

Alias for squeue -u $USER

Sometimes one has to know which jobs are still running and this is done by squeue -u $USER. This is tough to type, and writing sq for that is easier.

alias sq="squeue -u $USER"