Examples not working?

Useful Job Environment Variables #

The scheduler sets variables in the environment of the job script. Here is an excerpt of the most useful ones:

When job arrays are used:

For further details, see Section ‘OUTPUT ENVIRONMENT VARIABLES’ of man sbatch.

Example #

To see all environment variable available to a job, we can submit a quick 30-second, 10-MiB job that outputs all environment variables and their values sorted by name;

$ sbatch --mem=10M --time=00:00:30 --wrap="env | sort"
Submitted batch job 3076

Note how we used the --wrap option, which avoids having to create a job script when doing small tasks like here.

When finished, check the content of the output file:

$ grep -E "(HOSTNAME|PWD|TMPDIR|SLURM_)" slurm-3076.out
HOSTNAME=c4-n10
PWD=/c4/home/alice
SLURM_CLUSTER_NAME=c4
SLURM_CONF=/etc/slurm/slurm.conf
SLURM_CPUS_ON_NODE=1
SLURM_GTIDS=0
SLURM_JOB_ACCOUNT=boblab
SLURM_JOB_CPUS_PER_NODE=1
SLURM_JOB_GID=500
SLURM_JOB_ID=3076
SLURM_JOBID=3076
SLURM_JOB_NAME=wrap
SLURM_JOB_NODELIST=c4-n10
SLURM_JOB_NUM_NODES=1
SLURM_JOB_PARTITION=common
SLURM_JOB_QOS=normal
SLURM_JOB_UID=30000
SLURM_JOB_USER=alice
SLURM_LOCALID=0
SLURM_MEM_PER_NODE=10
SLURM_NNODES=1
SLURM_NODE_ALIASES=(null)
SLURM_NODEID=0
SLURM_NODELIST=c4-n10
SLURM_PRIO_PROCESS=0
SLURM_PROCID=0
SLURM_SUBMIT_DIR=/c4/home/alice
SLURM_SUBMIT_HOST=c4-log1
SLURM_TASK_PID=17170
SLURM_TASKS_PER_NODE=1
SLURM_TOPOLOGY_ADDR=c4-n10
SLURM_TOPOLOGY_ADDR_PATTERN=node
SLURM_WORKING_CLUSTER=c4:10.10.10.3:6817:8960:101
TMPDIR=/scratch/alice/3076

Environment variables in different languages #

Here are some examples how to get the value of environment variable SLURM_NTASKS in some of the most popular programming languages. The value is assigned to a local variable nslots, and if not set, 1 is used as the default value. All examples coerce the value to a numeric value and then outputs a message with the value.

Bash #

nslots=${SLURM_NTASKS:-1}
echo "Number of slots available: ${nslots}"

Python #

import os
nslots = os.getenv('SLURM_NTASKS', '1')  # env var is always a 'str'
nslots = int(nslots)                     # coerce to 'int'
print('Number of slots available: ' + nslots)

R #

nslots <- Sys.getenv("SLURM_NTASKS", "1")  # env var is always a 'character'
nslots <- as.integer(nslots)               # coerce to 'integer'
message("Number of slots available: ", nslots)

Ruby #

nslots = ENV["SLURM_NTASKS"] || "1"  # env var is always a 'String'
nslots = nslots.to_i                 # coerce to 'Integer'
puts "Number of slots available: #{nslots}"