Skip to content

SLURM Environment Variables

The SLURM controller will set variables in the environment of the batch script. Below is a list of SLURM variables that you may use within your job script. Some variables are only defined if their corresponding options were invoked, such as those pertaining to job arrays or task and CPU configurations. See the curated list of job specifications for more on these options.

SLURM Variables Description
SLURM_ARRAY_TASK_COUNT Total number of tasks in a job array
SLURM_ARRAY_TASK_ID Job array ID (index) number
SLURM_ARRAY_TASK_MAX Job array's maximum ID (index) number
SLURM_ARRAY_TASK_MIN Job array's minimum ID (index) number
SLURM_ARRAY_TASK_STEP Job array's index step size
SLURM_ARRAY_JOB_ID Job array's master job ID number
SLURM_CLUSTER_NAME Name of the cluster on which the job is executing
SLURM_CPUS_ON_NODE Number of CPUS on the allocated node
SLURM_CPUS_PER_TASK Number of cpus requested per task. Only set if the --cpus-per-task option is specified.
SLURM_JOB_ACCOUNT Account name associated of the job allocation
SLURM_JOBID, SLURM_JOB_ID The ID of the job allocation
SLURM_JOB_CPUS_PER_NODE Count of processors available to the job on this node.
SLURM_JOB_DEPENDENCY Set to value of the --dependency option
SLURM_JOB_NAME Name of the job
SLURM_NODELIST, SLURM_JOB_NODELIST List of nodes allocated to the job
SLURM_NNODES, SLURM_JOB_NUM_NODES Total number of different nodes in the job's resource allocation
SLURM_MEM_PER_NODE Takes the value of --mem if this option was specified.
SLURM_MEM_PER_CPU Takes the value of --mem-per-cpu if this option was specified.
SLURM_NTASKS, SLURM_NPROCS Same as -n or --ntasks if either of these options was specified.
SLURM_NTASKS_PER_NODE Number of tasks requested per node. Only set if the --ntasks-per-node option is specified.
SLURM_NTASKS_PER_SOCKET Number of tasks requested per socket. Only set if the --ntasks-per-socket option is specified.
SLURM_SUBMIT_DIR The directory from which sbatch was invoked
SLURM_SUBMIT_HOST The hostname of the computer from which sbatch was invoked
SLURM_TASK_PID The process ID of the task being started
SLURMD_NODENAME Name of the node running the job script
SLURM_JOB_GPUS GPU IDs allocated to the job (if any).

Setting Environment Variables in SLURM Jobs

You may also set your own variables for use in your SLURM jobs. One way is to set them inside the script itself, but that requires modifying the script.

It is possible to pass variables into a SLURM job when you submit the job using the --export flag. For example to pass the value of the variables REPS and X into the job script named jobs.sb you can use:

1
sbatch --export=REPS=500,X='test' jobs.sb

These are then available in your jobs as $REPS and $X

Using variables to set SLURM job name and output files

SLURM does not support using arbitrary variables in the #SBATCH lines within a job script; for example, #SBATCH -N=$REPS will not replace $REPS with the variable's value.

For specifying filenames (such as the SLURM log/output file), a limited number of pre-defined variables are available. These include %j, which references the job ID. For example, you can use #SBATCH --output=analysis-%j.out to set a custom output filename that includes the job number. The full list of these variables is available in the sbatch documentation

Job options specified from the command line have precedence over values defined in the job script and you can set certain SLURM variables in the command line. For example, you could set the job name and output/error files from the sbatch command line:

1
2
3
4
5
RUNTYPE='test'
RUNNUMBER=5
sbatch --job-name=$RUNTYPE.$RUNNUMBER.run \
--output=$RUNTYPE.$RUNUMBER.txt \
--export=A=$A,b=$b jobscript.sbatch

However note in this example, the output file doesn't have the job ID, which is not available from the command line. The job ID is only defined inside the environment created when running the batch script.