Batch Jobs
Submitting jobs using batch scripts
If you'd prefer to see the following instructions in a video, we have a tutorial on batch job submission on Oscar.
To run a batch job on Oscar, you first have to write a script that describes what resources you need and how your program will run. Some example batch scripts are available in your home directory on Oscar, in the directory:
A batch script starts by specifying the bash
shell as its interpreter with the line:
By default, a batch job will reserve 1 core and 2.8GB of memory per core for your job. You can customize the amount of resources allocated for your job by explicitly requesting them in your batch script with a series of lines starting with #SBATCH
, e.g.,
The above lines request 4 cores (-n
), 16GB of memory per node (--mem
), and one hour of runtime (-t
). After you have described the resources you want allocated for the job, you then give the commands that you want to be executed.
All of the #SBATCH
instructions in your batch script must appear before the commands you want to run.
Once you have your batch script, you can submit a batch job to the queue using the sbatch
command:
Submitting jobs from the command line
As an alternative to requesting resources within your batch script, it is possible to define the resources requested as command-line options to sbatch
. For example, the command below requests 4 cores (-n
), 16GB of memory per node (--mem
), and one hour of runtime (-t
) to run the job defined in the batch script.
Note that command-line options passed to sbatch
will override the resources specified in the script, so this is a handy way to reuse an existing batch script when you just want to change a few of the resource values.
Output from batch jobs
The sbatch
command will return a number, which is your Job ID. You can view the output of your job in the file slurm-<jobid>.out
in the directory where you invoked the sbatch
command. For instance, you can view the last 10 lines of output with:
Alternatively, you can mention the file names where you want to dump the standard output and errors using the -o
and -e
flags. You can use %j
within the output/error filenames to add the id of the job. If you would like to change your output file to be MyOutput-<job-id>, you can add the following line to your batch job:
sbatch command options
A full description of all of the options forsbatch
can be found online or by using the following command on Oscar:
The table below summarizes some of the more useful options forsbatch
.
Passing environment variables to a batch job
When a user logs into Oscar, there are pre-set environment variables such as HOME, which are the user's login environment variables. A user may modify an existing enviornmet variable, or add a new environment variable. So when a user submits a slurm batch job, the user's current environment variables may differ from the user's login environment. By default, a user's current environment variables, instead of the user's login environment variables, are accessible to the user's batch jobs on Oscar.
To modify or add an environment variable, run the following command:
run the following command in your shell
or have the following line in your batch script
After the step above to modify or add an environment variable, your batch job can access the environment variable my_variable whose value is my_value.
To export more than one environment variables, just list all the name=value pairs separated by commas:
Here is an example that a batch script loops over an input file and submits a job for each directory in the input file, where a directory is passed to a batch job for processing.
The input file test.txt has multiple lines where each line is a directory:
The loop.sh script reads each line (directory) from the input file and passes the directory as an environment variable to a batch job:
The test.job is a job script, which runs the test.sh to process the directory passed as an environment variable:
The test.sh is a bash script which simply echoes the directory:
If you run ./loop.sh
, then three jobs are submitted. Each job generates an output like the following:
Using variables to set slurm job name, output filename, and error filename
Variables can be passed at the sbatch command line to set the job name, output and error file names, as shown in the following example:
Last updated