Skip to content

Submitting Jobs

Exercises

  1. Submit with a custom output filename

Write a short job script that runs echo "Hello from $HOSTNAME". Use the --output directive to write output to logs/%x_%j.out (job name + job ID). Remember to create the logs/ directory first.

Hint / Solution
mkdir -p logs

cat > custom_output.sh << 'EOF'
#!/bin/bash
#SBATCH --job-name=custom_out
#SBATCH --output=logs/%x_%j.out
#SBATCH --time=00:05:00
#SBATCH --ntasks=1
#SBATCH --mem=1G

echo "Hello from $HOSTNAME"
EOF

sbatch custom_output.sh
Expected output: Validated 2026-10-19 on slurm-training-val (Slurm 25.11.8, ParallelCluster 3.16.1).
$ sbatch custom_output.sh
Submitted batch job 42
  1. Use --wrap for a one-liner

Submit a quick job using --wrap that prints the current date and the contents of /proc/cpuinfo | head -5. Request 5 minutes and 1G of memory on the command line.

Hint / Solution
sbatch --time=00:05:00 --mem=1G --wrap="date; head -5 /proc/cpuinfo"
Expected output: Validated 2026-10-19 on slurm-training-val (Slurm 25.11.8, ParallelCluster 3.16.1).
$ sbatch --time=00:05:00 --mem=1G --job-name=val_sj2 --output=/dev/null --wrap='date; head -5 /proc/cpuinfo'
Submitted batch job 42
  1. Set up email notifications

Modify a job script (or use command-line flags) to send you an email when the job begins, ends, or fails. Submit it and verify the directives with scontrol show job.

Hint / Solution
sbatch --mail-type=BEGIN,END,FAIL --mail-user=you@example.com --time=00:05:00 --wrap="sleep 30"

# Verify the settings
scontrol show job <jobid> | grep -i mail
Expected output: Validated 2026-10-19 on slurm-training-val (Slurm 25.11.8, ParallelCluster 3.16.1).
$ sbatch --mail-type=BEGIN,END,FAIL --mail-user=you@example.com --time=00:05:00 --job-name=val_sj3 --wrap='sleep 30'
Submitted batch job 42

$ scontrol show job 42 | grep -i mail
   SubmitLine=sbatch --mail-type=BEGIN,END,FAIL --mail-user=you@example.com --time=00:05:00 --job-name=val_sj3 --wrap=sleep 30
   MailUser=you@example.com MailType=BEGIN,END,FAIL
  1. Pass arguments to a job script

Create a job script called greet.sh that accepts two positional arguments: a name ($1) and a project ($2). Have it print "Hello, [name]! Working on [project]." Submit it with arguments.

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

NAME=$1
PROJECT=$2
echo "Hello, $NAME! Working on $PROJECT."
EOF

sbatch greet.sh "Alice" "genome_assembly"
Expected output: Validated 2026-10-19 on slurm-training-val (Slurm 25.11.8, ParallelCluster 3.16.1).
$ sbatch greet.sh Alice genome_assembly
Submitted batch job 42
  1. Use --chdir to control the working directory

Create a directory called /tmp/$USER/test_chdir. Submit a job that uses --chdir to set the working directory to that path. Inside the script, run pwd to confirm the job started in the right directory.

Hint / Solution
mkdir -p /tmp/$USER/test_chdir

sbatch --chdir=/tmp/$USER/test_chdir --time=00:05:00 --wrap="pwd; echo 'Working dir test' > chdir_test.txt"

# After the job completes, check:
cat /tmp/$USER/test_chdir/chdir_test.txt
Expected output: Validated 2026-10-19 on slurm-training-val (Slurm 25.11.8, ParallelCluster 3.16.1).
$ sbatch --chdir=/home/ec2-user/slurm-val-chdir --time=00:05:00 --job-name=val_sj5        --output=chdir_%j.out        --wrap='pwd > chdir_test.txt'
Submitted batch job 42

$ cat /home/ec2-user/slurm-val-chdir/chdir_test.txt
/home/ec2-user/slurm-val-chdir
  1. Catch a time-limit warning signal

Submit a job with a 2-minute time limit and --signal=B:USR1@30. Its trap handler writes a checkpoint file and exits 0. After the job ends, confirm the file exists and the job completed cleanly.

Hint / Solution
cat > ckpt.sh << 'EOF'
#!/bin/bash
#SBATCH --job-name=ckpt_test
#SBATCH --output=ckpt_%j.out
#SBATCH --time=00:02:00
#SBATCH --signal=B:USR1@30

on_usr1() {
    echo "checkpoint written $(date +%T)" > ckpt_${SLURM_JOB_ID}.txt
    exit 0
}
trap on_usr1 USR1

sleep 600 &
wait
EOF

sbatch ckpt.sh

# After ~2 minutes:
cat ckpt_<jobid>.txt
sacct -j <jobid> --format=JobID,State,ExitCode
Expect `COMPLETED` and `0:0`: the handler exited before the time limit, so the job was not killed. Expected output: Validated 2026-10-19 on slurm-training-val (Slurm 25.11.8, ParallelCluster 3.16.1).
$ sbatch ckpt.sh
Submitted batch job 42

$ cat /shared/ec2-user/val_ckpt_42.txt
checkpoint written 01:27:44

$ sacct -j 42 -X -n -P -o JobID,State,ExitCode
42|COMPLETED|0:0

References