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-04-15 on slurm-training-val (Slurm 25.11.4, ParallelCluster 3.15.0).
$ 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-04-15 on slurm-training-val (Slurm 25.11.4, ParallelCluster 3.15.0).
$ 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-04-15 on slurm-training-val (Slurm 25.11.4, ParallelCluster 3.15.0).
$ 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-04-15 on slurm-training-val (Slurm 25.11.4, ParallelCluster 3.15.0).
$ 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-04-15 on slurm-training-val (Slurm 25.11.4, ParallelCluster 3.15.0).
$ 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

References