When working with Slurm, the job environment variables available to your job depend on when and how the job is submitted. Here’s how you can access and understand Slurm job environment variables from the master node (the login node where you submit jobs):
When Slurm runs your job, it sets several environment variables that describe the job’s context. These are available inside the job script on the compute nodes.
SLURM_JOB_IDSLURM_JOB_NAME--job-name).SLURM_JOB_NODELISTSLURM_JOB_NUM_NODESSLURM_NTASKSSLURM_NPROCSSLURM_CPUS_PER_TASKSLURM_MEM_PER_NODESLURM_PARTITIONSLURM_TIMELIMITSLURM_WORKING_DIRSLURM_SUBMIT_DIRSLURM_EXIT_CODEIf you submit a job using a script (e.g., sbatch), you can access these variables directly in your script:
bash#!/bin/bash #SBATCH --job-name=my_job #SBATCH --nodes=1 #SBATCH --ntasks=1 #SBATCH --cpus-per-task=4 #SBATCH --mem=8G #SBATCH --time=01:00:00 echo "Job ID: $SLURM_JOB_ID" echo "Job Name: $SLURM_JOB_NAME" echo "Node List: $SLURM_JOB_NODELIST" echo "Number of Nodes: $SLURM_JOB_NUM_NODES" # ... and so on
You can also check the environment variables of a running job from the master node using:
bashscontrol show job <JOB_ID> -o
or
bashsqueue -j <JOB_ID> --format="%i %j %N %P %T %M %D %m %c"
If you want to pass environment variables from the master node to the job, you can use:
bashexport MY_VAR="value" sbatch --export=ALL,MY_VAR my_script.sh
or
bashsbatch --export=MY_VAR1,MY_VAR2 my_script.sh
If you want to inspect the environment of a running job, you can use:
bashsrun --jobid=<JOB_ID> env
SLURM_JOB_IDSLURM_JOB_NAMESLURM_JOB_NODELISTSLURM_NTASKSSLURM_NPROCSSLURM_PARTITIONSLURM_TIMELIMITSLURM_WORKING_DIRbash#!/bin/bash #SBATCH --job-name=env_test #SBATCH --nodes=1 #SBATCH --ntasks=1 echo "All Slurm environment variables:" env | grep SLURM
Let me know if you want to see the output of a specific command or need help with a concrete example!