Skip to content

Job Dependencies

Exercises

  1. Create a 2-job pipeline with afterok

Write two simple scripts: step1.sh that creates a file called step1_done.txt, and step2.sh that checks for that file and prints "Pipeline complete." Submit them so that step 2 only runs after step 1 succeeds. Use --parsable to capture the job ID.

Hint / Solution
cat > step1.sh << 'EOF'
#!/bin/bash
#SBATCH --job-name=step1
#SBATCH --output=step1_%j.out
#SBATCH --time=00:05:00

echo "Step 1 running"
touch step1_done.txt
EOF

cat > step2.sh << 'EOF'
#!/bin/bash
#SBATCH --job-name=step2
#SBATCH --output=step2_%j.out
#SBATCH --time=00:05:00

if [ -f step1_done.txt ]; then
    echo "Pipeline complete. step1_done.txt exists."
else
    echo "ERROR: step1_done.txt not found"
    exit 1
fi
EOF

JOB1=$(sbatch --parsable step1.sh)
JOB2=$(sbatch --parsable --dependency=afterok:$JOB1 step2.sh)
echo "Step 1: $JOB1, Step 2: $JOB2"

# Verify the dependency chain
squeue --me -o "%.8i %.20j %.2t %E"
  1. Fan-in: Job C depends on both A and B

Submit two independent jobs (A and B) that each sleep for 30 seconds. Then submit job C with a dependency on both A and B succeeding. Verify with squeue that C shows both dependencies.

Hint / Solution
JOBA=$(sbatch --parsable --time=00:05:00 --job-name=jobA --wrap="sleep 30; echo 'A done'")
JOBB=$(sbatch --parsable --time=00:05:00 --job-name=jobB --wrap="sleep 30; echo 'B done'")
JOBC=$(sbatch --parsable --dependency=afterok:${JOBA},afterok:${JOBB} \
    --time=00:05:00 --job-name=jobC --wrap="echo 'C: both A and B finished'")

echo "A=$JOBA B=$JOBB C=$JOBC"
squeue --me -o "%.8i %.20j %.2t %E"
  1. Use singleton dependency

Submit two jobs with the same job name and --dependency=singleton. The second job should wait until the first one completes before starting. Verify by checking squeue to see the second job pending with reason (Dependency).

Hint / Solution
sbatch --time=00:05:00 --job-name=singleton_test --wrap="echo 'First run'; sleep 60"
sbatch --time=00:05:00 --job-name=singleton_test --dependency=singleton --wrap="echo 'Second run'"

squeue --me -o "%.8i %.20j %.2t %R"
# Second job should show PD with (Dependency)
  1. Handle a failed dependency

Submit a job (step 1) that intentionally fails (exit 1). Then submit a step 2 with afterok dependency and a separate error handler with afternotok dependency. Observe that step 2 never runs (state DependencyNeverSatisfied) while the error handler does run.

Hint / Solution
JOB1=$(sbatch --parsable --time=00:05:00 --job-name=will_fail --wrap="echo 'Failing now'; exit 1")
JOB2=$(sbatch --parsable --dependency=afterok:$JOB1 --time=00:05:00 \
    --job-name=next_step --wrap="echo 'This should not run'")
JOB3=$(sbatch --parsable --dependency=afternotok:$JOB1 --time=00:05:00 \
    --job-name=error_handler --wrap="echo 'Handling failure from job $JOB1'")

echo "Fail=$JOB1 Next=$JOB2 Handler=$JOB3"

# After JOB1 fails, check:
squeue --me -o "%.8i %.20j %.2t %R"
# JOB2 should show DependencyNeverSatisfied; JOB3 should run

# Clean up JOB2 since it will never run
scancel $JOB2

References