Skip to content

Example 2. Run a python script using sbatch

Sbatch file

This is the run.sh.

#!/bin/bash

#SBATCH --time=00:04:00
#SBATCH --ntasks=2 # 2 tasks per job
#SBATCH --cpus-per-task=1
#SBATCH --mem=3000M # Total memory for the job (1500M * 2 tasks)
#SBATCH --partition=serc
#SBATCH --array=0-4 # Array from index 0 to 4, creating 5 jobs in total
#SBATCH -o /scratch/users/bastudil/ignore/%j_%a_slurm.out # Separate STDOUT for each array task


#----------------
# Load any modules or environments with preinstalled requirements
module load python/3.9.0
source "/home/users/bastudil/myproject_env/bin/activate"


#----------------
# Define the custom array of PIDs

myPID=(0 1 2 3 4 5 6 7 8 9 10)


#----------------
# Loop through each task manually to ensure each gets a unique PID

for task_id in $(seq 0 $((SLURM_NTASKS - 1))); do
    # Calculate a unique index for each task within the array job
    index=$((SLURM_ARRAY_TASK_ID * SLURM_NTASKS + task_id))

    # Check to ensure index is within bounds of myPID array
    if (( index < ${#myPID[@]} )); then

        #----------------
        # Current pid
        pid=${myPID[$index]}

        #----------------
        # Run the Python script with srun and pass the unique PID
        srun --ntasks=1 --exclusive python3 run.py -myPID "$pid" &


    fi
done




# Wait for all background tasks to complete
wait

Python file

This is the run.py

# You are in python now :)

import openseespy.opensees as ops
import os, sys
import time
import itertools


def yourcode():
    # input arguments
    Arg1 = sys.argv[1]
    Arg2 = sys.argv[2]

    # your python code here

    # You are welcome


def main():
    print("run.py started at: ",datetime.datetime.now())

    yourcode()

    print("run.py ended at: ",datetime.datetime.now())


if __name__ == "__main__":
    main()