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
  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"
  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
  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"
  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

References