Search code examples
bashheredocslurmsbatch

Use of a HEREDOC with SLURM sbatch --wrap


I am encountering difficulties using a (Bash) HEREDOC with a SLURM sbatch submission, via --wrap.

I would like the following to work:

SBATCH_PARAMS=('--nodes=1' '--time=24:00:00' '--mem=64000' '--mail-type=ALL')

sbatch ${SBATCH_PARAMS[@]} --job-name="MWE" -o "MWE.log" --wrap <<EOF
        SLURM_CPUS_ON_NODE=\${SLURM_CPUS_ON_NODE:-8}
        SLURM_CPUS_PER_TASK=\${SLURM_CPUS_PER_TASK:-\$SLURM_CPUS_ON_NODE}
        export OMP_NUM_THREADS=\$SLURM_CPUS_PER_TASK

        parallel --joblog "MWE-jobs.log" --resume --resume-failed -k --linebuffer -j \$((\$OMP_NUM_THREADS/4)) --link "MWE.sh {1} {2}" ::: "./"*R1*.fastq.gz ::: "./"*R2*.fastq.gz
EOF

On my current cluster, sbatch returns the below error, refusing to submit this job:

ERROR:   option --wrap requires argument

Might anyone know how I can get this to work?


Solution

  • Since wrap expects a string argument, you can't use a heredoc directly. Heredocs are used when a filename is expected where it's undesirable to make one.

    Use a heredoc for cat, where it does expect a filename, and use its output as the string for which --wrap expects:

    SBATCH_PARAMS=('--nodes=1' '--time=24:00:00' '--mem=64000' '--mail-type=ALL')
    
    sbatch ${SBATCH_PARAMS[@]} --job-name="MWE" -o "MWE.log" --wrap $(cat << EOF
            SLURM_CPUS_ON_NODE=\${SLURM_CPUS_ON_NODE:-8}
            SLURM_CPUS_PER_TASK=\${SLURM_CPUS_PER_TASK:-\$SLURM_CPUS_ON_NODE}
            export OMP_NUM_THREADS=\$SLURM_CPUS_PER_TASK
    
            parallel --joblog "MWE-jobs.log" --resume --resume-failed -k --linebuffer -j \$((\$OMP_NUM_THREADS/4)) --link "MWE.sh {1} {2}" ::: "./"*R1*.fastq.gz ::: "./"*R2*.fastq.gz
    EOF)