How to Write a shell alias or function for quick login to a node managed by SLURM#

For tasks better run in a dedicated but interactive fashion, it might be advantageous to save the repeating pattern of reserving resources and starting a new associated shell in an alias or function, as explained below.

If you use bash as default shell you can place the following alias definition in your ~/.bashrc file and source this file in the ~/.bash_profile or in the ~/.profile file:

alias ssh2node='salloc -n 1 -p prepost -- /bin/bash -c "ssh -tt -X \$SLURM_JOB_NODELIST"'

If you use tcsh as default shell you can put the following line in your ~/.cshrc file to define the alias ssh2node:

alias ssh2node 'salloc -n 1 -p prepost -- /bin/tcsh -c '\''ssh -X $SLURM_JOB_NODELIST'\'''

Thereafter, the command ssh2node can be used to allocate computing resources and log into the allotted node in one step:

$ ssh2node
salloc: Pending job allocation 292893
salloc: job 292893 queued and waiting for resources
salloc: job 292893 has been allocated resources
salloc: Granted job allocation 292893
Warning: Permanently added 'm11515,10.50.9.252' (RSA) to the list of known hosts.
user@m11515:~>

An even more flexible solution is to define a shell function that combines ‘salloc’ and ‘ssh’ and accepts options to specify those requirements likely to change (e.g. number of tasks, partition, project account).

The example below defines a bash function s2n that can be added to your ~/.bashrc file:

s2n(){
    # set default values for number of tasks, partition, and account
    ntasks=1
    partition=prepost
    account=$SLURM_ACCOUNT
    OPTIND=1
    # parse options with arguments
    while getopts :n:p:a: opt; do
        case ${opt} in
            n ) ntasks=$OPTARG
                ;;
            p ) partition=$OPTARG
                ;;
            a ) account=$OPTARG
                ;;
           \? ) echo "Invalid option: $OPTARG" 1>&2
                return
                ;;
            : ) echo "Invalid option: $OPTARG requires an argument" 1>&2
                return
                ;;
        esac
    done
    # allocate ressources and log into the allotted node
    salloc -A ${account} -n ${ntasks} -p ${partition} -- /bin/bash -c "ssh -tt -X \$SLURM_JOB_NODELIST"
}

and used to start interactive sessions according to different needs, for example:

# ask for 4 CPUs on one node in the partition prepost using default account
$ s2n -n 4 -p prepost

# use account xz0456 instead of default account
$ s2n -a xz0456

# ask for 48 CPUs on one node in the partition gpu
$ s2n -n 48 -p gpu

Tcsh or csh do not allow for shell functions. This deficiency can be partly compensated for by defining an alias with positional parameters as shown in the example below:

alias s2n 'salloc -n \!:1 -p \!:2 -A \!:3 -- /bin/tcsh -c '\''ssh -X $SLURM_JOB_NODELIST'\'''

However, for tcsh/csh, all parameters MUST be specified and the order of parameters (in the example above number of tasks, partition, account) DOES matter:

# ask for 4 CPUs on on node in the partition prepost using default account
$ s2n 4 prepost xz0123

# use account xz0456 instead of default account
$ s2n 1 prepost xz0456

# ask for 48 CPUs on one node in the partition gpu
$ s2n 48 gpu xz0123

Another possibility (not shown here) is to write a Unix utility using a language of your choice (python, perl etc.), place it in your ~/bin directory and add this directory to your PATH.