Capturing the email address used in a SLURM batch script

Viewed 191

I have a SLURM (v19.05.6) batch script :

test.sh :

#!/bin/bash
#SBATCH --mail-user=First.Last@domain.com
#SBATCH --mail-type=ALL
env

Within my script, I'd like to be able to get email address (i.e. First.Last@domain.com) that SLURM uses to mail the user. I printed out the environment (i.e. env) and it is not set as an environmental variable. Looking at the man page for sacct doesn't contain a field for email address either.

QUESTION :

How do I get the email address used by SLURM to email users? In my use case, I'd like to capture that email address and use it in subsequently spawned jobs.

1 Answers

You could always try to grep it from the job script:

MAIL=`grep -m 1 "mail-user=" $0 | cut -d = -f2`

the -m 1 stops grep at the first match and $0 resolves to the path of the script.

I think it is even my preferred version, as it doesn't need extra communication with the slurmctld. For newer versions (20.02+), you can also parse the output from scontrol:

MAIL=`scontrol show job ${SLURM_JOB_ID} | grep MailUser |  tr -s ' ' | cut -d ' ' -f 2 | cut -d = -f 2`
Related