Search code examples
bashqueueslurm

How is the `-l` in the header `#!/bin/bash -l` different from using #SBATCH --get-user-env=L?


The -l flag in this job file refers to executing as if it was in the login node right? Meaning that it will first read ~/.bash_profile. How is this different from using #SBATCH --get-user-env=L?

Will the latter flag only read environment variables and not the rest from ~/.bash_profile?

Not sure whether is it better to use one or the other. Also don't know if --export NONE will override both of these options.

#!/bin/bash -l

#SBATCH --account project_id 
#SBATCH --mail-type ALL     
#SBATCH --chdir /scratch/<your_username>/
#SBATCH --job-name my_code 
#SBATCH --output my_code.out
#SBATCH --partition cpu
#SBATCH --nodes 1 
#SBATCH --ntasks 1 
#SBATCH --cpus-per-task 8 
#SBATCH --mem 10G 
#SBATCH --time 00:30:00 
#SBATCH --export NONE
#SBATCH --get-user-env=L

Solution

  • After looking at the docs, it seems like they do the same thing.

    But in the end, the behavior depends on how Slurm executes your script. You may be aware that there are two different ways you can execute a Bash script yourself:

    ./script.sh
    
    bash ./script.sh
    

    If Slurm does the equivalent of the former, then that means that your -l flag takes effect and the script is ran by a Bash login shell. In this case, the Linux kernel looks at the file, looks at the shebang, and runs Bash with the -l argument.

    However, if Slurm does the equivalent of the latter, then that means your -l flag has no effect. In this case, Bash reads the file, and ignores the first line because it is a comment.

    Therefore, I would recommend setting #SBATCH --get-user-env=L, because that is a guarantee by Slum that the script will executable in a login shell. Adding -l may not have an effect (one would have to look at the source code to be sure, and even then, it is possible the behavior can change since it is an implementation detail).