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

Dmitri Chebotarov dchebota at gmu.edu
Thu May 10 14:11:44 MDT 2018


Thank you all for suggestions... 

I will take closer look at 'exec sbatch...' script.
Running ''sbatch --output.. --error=..' works as well so far.

´╗┐On 5/10/18, 11:14 , "slurm-users on behalf of Michael Jennings" <slurm-users-bounces at lists.schedmd.com on behalf of mej at lanl.gov> wrote:

    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:
    
    #!/bin/bash
    
    exec sbatch "$@" <<-EOF
    	#!/bin/bash
    	#SBATCH --output=$SCRATCH/slurm/%j.out
    	#SBATCH --error=$SCRATCH/slurm/%j.err
    	...
    EOF
    
    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. :-)
    
    HTH,
    Michael
    
    -- 
    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