1. First, create a new file in the current working directory, called my-first-batch-script.sh : gedit my-first-sbatch-script.sh
2. Enter the following lines in the new file you just created:
#!/bin/bash
#SBATCH --time=05:00:00
#SBATCH --ntasks=2
#SBATCH --mem=1G
Submitting this script to slurm as is, will allocate for a time limit of 5 hours, 2 CPU cores and 1GB of RAM memory.
But - it will immediately terminate since no actual commands are issued
3. After these slurm options in the script you can enter any bash command that is required for running your code, set environment variables and load modules.
In this example, we will add the following commands to the sbatch script. This runs script.R after loading R4 version 4.1.0
module load R4/4.1.0
srun Rscript /path/to/my/R/script.R
The final script will be:
#!/bin/bash
#SBATCH --time=05:00:00
#SBATCH --ntasks=2
#SBATCH --mem=1G
module load R4/4.1.0
srun Rscript /path/to/my/R/script.R
Remember to save it !
4. Once the script is saved, we can submit it by typing: sbatch my-first-sbatch-script.sh
This will show the following output in the shell: Submitted batch job <job id>, indicating that the job has been submitted and assigned a job ID.
After the job has been submitted and has an ID, sbatch will exit.
5. Even though the job has been submitted and sbatch has exited, the job has not necessarily been granted resources at this point.
It may sit in the queue of pending jobs for some time before its required resources become available.
ASKOREN: what does the following mean?
Note: the srun command is used before calling the actual program executable or bash command.
Properly constructed sbatch scripts execute all commands using the srun command to ensure that slurm is aware of all job steps.
This also allows for more advanced distribution of resources within the scripts, for example:
#!/bin/bash
#SBATCH --time=05:00:00
#SBATCH --ntasks=2
#SBATCH --cpu
#SBATCH --mem=1G
module load R4/4.1.0
srun Rscript /path/to/my/R/script.R