38

When I launch a computation on the cluster, I usually have a separate program doing the post-processing at the end :

sbatch simulation
sbatch --dependency=afterok:JOBIDHERE postprocessing

I want to avoid mistyping and automatically have the good job id inserted. Any idea? Thanks

user1824346
  • 575
  • 1
  • 6
  • 17

1 Answers1

54

You can do something like this:

RES=$(sbatch simulation) && sbatch --dependency=afterok:${RES##* } postprocessing

The RES variable will hold the result of the sbatch command, something like Submitted batch job 102045. The construct ${RES##* } isolates the last word (see more info here), in the current case the job id. The && part ensures you do not try to submit the second job in the case the first submission fails.

damienfrancois
  • 52,978
  • 9
  • 96
  • 110
  • 34
    Note that in recent versions of Slurm, `sbatch` has a `--parsable` parameter so that only the jobid is returned, simplifying this as `RES=$(sbatch --parsable simulation)` – damienfrancois Sep 28 '17 at 12:04
  • 3
    Beware that on a federated cluster, `sbatch` will give the name of the cluster in addition to the job id. – damienfrancois Apr 01 '19 at 12:24
  • 1
    Note, using `--parsable` flag you might get a comma separated list. From the man page of `sbatch`: `--parsable` Outputs only the job id number and the cluster name if present. The values are separated by a semicolon. Errors will still be displayed. – Stefan Aug 07 '20 at 09:56
  • 1
    Another method would be to include in your script something like echo $SLURM_JOB_ID this will echo the exported env variable – Roman Gherta Jun 28 '21 at 09:59