[slurm-users] How to access environment variables in submit script?

Michael Jennings mej at lanl.gov
Thu May 10 09:10:34 MDT 2018

On Thursday, 10 May 2018, at 10:09:22 (-0400),
Paul Edmon wrote:

> Not that I am aware of.  Since the header isn't really part of the
> script bash doesn't evaluate them as far as I know.
> On 05/10/2018 09:19 AM, Dmitri Chebotarov wrote:
> >
> >Is it possible to access environment variables in a submit script?
> >F.e. $SCRATCH is set to a path and I like to use $SCRATCH variable in #SBATCH:
> >
> >#SBATCH --output=$SCRATCH/slurm/%j.out
> >#SBATCH --error=$SCRATCH/slurm/%j.err
> >
> >Since it's Bash script, # are ignored and I suspect these variables
> >need to be defined on Slurm controller (?).
> >
> >As a workaround I can run 'sbatch --output=$SCRATCH/slurm/%j.out
> >--error=$SCRATCH/slurm/%j.err <myscript>' and it works. But is there
> >any way to use non-slurm env variables via #SBATCH?

Not directly in a BASH script file, no.  Lines beginning with the
octothorpe/hash/pound sign ('#') are summarily ignored by the shell --
that's why they can be used to provide metadata to other programs!

However, you can feed commands to a separate shell using a script, or
create and run a distinct shell script:


exec sbatch "$@" <<-EOF
	#SBATCH --output=$SCRATCH/slurm/%j.out
	#SBATCH --error=$SCRATCH/slurm/%j.err

Note that you have to run the script directly, NOT via sbatch, for it
to work -- it does the sbatch execution on its own.

And if you *always* use those options with sbatch, you can always
alias sbatch to "sbatch --output=... --error=..." or something along
those lines. :-)


Michael E. Jennings <mej at lanl.gov>
HPC Systems Team, Los Alamos National Laboratory
Bldg. 03-2327, Rm. 2341     W: +1 (505) 606-0605

More information about the slurm-users mailing list