Difference between revisions of "Zsh"

From HPC Wiki
Jump to navigation Jump to search
(changed <code> to <pre>)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
The <pre>zsh</pre> offers a wide variety of possibile features to simplify command-line usage. If you're interested in a more user-friendly shell, look at [https://github.com/ohmyzsh/ohmyzsh 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.
+
The <code>zsh</code> 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 [https://github.com/ohmyzsh/ohmyzsh 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 <pre>~/.zshrc</pre>.
+
If you're interested in one of these, put them in your <code>~/.zshrc</code>.
  
Here are some useful functions for <pre>zsh</pre>, specific to Slurm-HPC-systems.
+
Here are some useful functions for <code>zsh</code>, specific to Slurm-HPC-systems.
  
  
Line 50: Line 50:
 
== ftails ==
 
== ftails ==
  
When using this, you will be able to follow jobs more easily. If you have just one sbatch job running, and enter <pre>ftails</pre>, 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 <pre>slurmlogpath</pre>, which, when called like <pre>slurmlogpath $SBATCH_ID</pre> will print the log path to stdout.
+
When using this, you will be able to follow jobs more easily. If you have just one sbatch job running, and enter <code>ftails</code>, 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 <code>slurmlogpath</code>, which, when called like <code>slurmlogpath $SBATCH_ID</code> will print the log path to stdout.
  
 
<pre>
 
<pre>
Line 79: Line 79:
 
== Alias for squeue -u $USER ==
 
== Alias for squeue -u $USER ==
  
Sometimes one has to know which jobs are still running and this is done by <pre>squeue -u $USER</pre>. This is tough to type, and writing <pre>sq</pre> for that is easier.
+
Sometimes one has to know which jobs are still running and this is done by <code>squeue -u $USER</code>. This is tough to type, and writing <code>sq</code> for that is easier.
  
 
<pre>
 
<pre>
 
alias sq="squeue -u $USER"
 
alias sq="squeue -u $USER"
 
</pre>
 
</pre>
 +
 +
[[Category:Basics]]

Latest revision as of 10:48, 17 October 2021

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"