20

I have the following script to submit job with slurm:

#!/bin/sh
#!/bin/bash
#SBATCH -J $3 #job_name 
#SBATCH -n 1 #Number of processors
#SBATCH -p CA 

nwchem $1 > $2

The first argument ($1) is my input, the second ($2) is my output and I would like the third ($3) to be my jobname. If I do like this, the job name is '$3'. How can I proceed to give the jobname as an argument of the script?

Thanks

Cabbage soup
  • 1,344
  • 1
  • 18
  • 26
Laetis
  • 1,337
  • 3
  • 16
  • 28

2 Answers2

23

The SBATCH directives are seen as comments by the shell and it does not perform variable substitution on $3. There are several courses of action:

Option 1: pass the -J argument on the command line:

sbatch -J thejobname submission_script.sh input.data output.res

Option 2: pass the script through stdin replacing the position arguments ($1, $2, etc. by named ones)

IN=input.data OUT=output.res NAME=thejobname <submission_script.sh sbatch 

Option 3: write a wrapper

#!/bin/bash
sbatch <<EOT
#!/bin/sh
#SBATCH -J $3 #job_name 
#SBATCH -n 1 #Number of processors
#SBATCH -p CA 

nwchem $1 > $2
EOT

and use it like this:

submit.sh input.data output.red thejobname

Also note that the second shebang (#!/bin/bash) is useless and ignored by the (parent) shell.

damienfrancois
  • 52,978
  • 9
  • 96
  • 110
0

Assuming that you have srun along with sbatch, you could run a srun one-liner within a sbatch script. Most of sbatch arguments can also be used with srun.

#!/bin/sh
#!/bin/bash
#SBATCH -n 1 #Number of processors
#SBATCH -p CA 

srun nwchem -J $3 $1 > $2
H.Alzy
  • 310
  • 4
  • 10