[Solved] Use Bash variable within SLURM sbatch script

This won’t work. What happens when you run

sbatch myscript.sh

is that slurm parses the script for those special #SBATCH lines, generates a job record, stores the batch script somewhere. The batch script is executed only later when the job runs.

So you need to structure you workflow in a slightly different way, and first calculate the number of procs you need before submitting the job. Note that you can use something like

sbatch -n $numProcs myscript.sh

, you don’t need to autogenerate the script (also, mpirun should be able to get the number of procs in your allocation automatically, no need to use “-np”).

Slurm stops processing #SBATCH directives on the first line of executable code in a script. For users whose #SBATCH directives are not dependent on the code they’re trying to run above those directives, just put the #SBATCH lines at the top.

See the other answer for a workaround/solution if, as with OP, your sbatch options are dependent on the commands you’ve placed above them.

The batch script may contain options preceded with “#SBATCH” before
any executable commands in the script
. sbatch will stop processing
further #SBATCH directives once the first non-comment non-whitespace
line has been reached in the script.

From the sbatch docs, my emphasis.

Read more here: Source link