Search code examples
linuxbashjobsslurm

Get Job id and put them into a bash command


Hello for a projet I need to execute a bash file only when all previous run have been finished so I use :

sbatch -d afterok:$JobID1:$JobID2:$JobIDN final.sh 

in Order to run the JobIDN I do

for job in Job*.sh ; do sbatch $job; done 

Then it prints all the jobIDs

I just wondered if someone haave a command in order to grab these IDs and put them directly to the command :

sbatch -d afterok:$JobID1:$JobID2:$JobIDN final.sh 

exemple

for job in Job*.sh ; do sbatch $job; done 
1
2
3


sbatch -d afterok:$1:$2:$3 final.sh 

Solution

  • You can do it with

    for job in Job*.sh ; do sbatch --parsable $job; done | paste -s -d: | xargs -I{} sbatch --depedency afterok:{} final.sh
    

    The paste command will gather all job IDS and write them on a single line, colon-separated, while xargs will take the result and insert it at the {} placeholder. The information about the job IDs is passed through pipes.