1

The Problem

I am attempting to create a script which automagically sets the directory in which to run an sbatch command with --chdir using a variable. My goal is to create a single template file that is easy for less experienced users to run my code with.

The Environment

In system I am running on, all users have a large file storage directory assigned to them which looks like /path/$USER. The path is not the $HOME directory and is not stored as an environmental variable elsewhere. All users are assigned a Slurm account where the Slurm username matches $USER. This means that the Slurm-specific variable %u is the same as $USER.

Minimal (non)-Working Example

#!/bin/bash

#SBATCH --partition=debug_5min
#SBATCH --ntasks=1
#SBATCH --mem=4000
#SBATCH --output=jobname_%J_stdout.txt
#SBATCH --error=jobname_%J_stderr.txt
#SBATCH --time=00:05:00
#SBATCH --job-name=user_pass
#SBATCH --chdir=/path/%u
#
################################################

touch testfile.txt

Result of MnWE

The batch executes, and no files (testfile.txt, stdout, stderr) are generated on run completion. If I do some digging on this test script, I am told that the run fails and that output was redirected to /tmp/.

What works, but doesn't achieve the goal

Instead of executing sbatch test.sh directly, I can pass --chdir prior to the script, allowing me to use $USER. The command looks like:

sbatch --chdir=/path/$USER test.sh

But this does not solve the original goal to make a script which does not require the end user to think too hard if they are scared of terminals.

The Question - Restated

Is there a way to use variables in --chdir sbatch headers?

Why is %u not behaving like I would expect?

WesH
  • 460
  • 5
  • 15

1 Answers1

2

One solution could be to replace the --chdir parameter with a call to the cd command and give absolute paths for the --output and --error directives:

#!/bin/bash

#SBATCH --partition=debug_5min
#SBATCH --ntasks=1
#SBATCH --mem=4000
#SBATCH --output=/path/%u/jobname_%J_stdout.txt
#SBATCH --error=/path/%u/jobname_%J_stderr.txt
#SBATCH --time=00:05:00
#SBATCH --job-name=user_pass
#
################################################
cd /path/$USER || exit -1 
touch testfile.txt

As for your restated questions, Bash will not expand variables in the #SBATCH ... lines as those are seen as comments by the interpreter, and Slurm makes no effort to do that job.

And the --chdir parameter simply does not accept patterns (like %u); only simple strings.

damienfrancois
  • 52,978
  • 9
  • 96
  • 110
  • 1
    Thanks for answering another Slurm directory question. The ``--chdir`` not accepting patterns is really unfortunate. Since other ``#SBATCH`` header options take variables, I wonder if this is a bug or intentional limitation of ``--chdir``. – WesH Dec 14 '20 at 17:50