Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using srun outside SLURM

Tags:

bash

slurm

I have a code that is usually run under SLURM. Something like

if ! lengthy_command
then
    echo "Error occured"
fi

For accounting purposes, I would like to move to a srun based launch of the command:

if ! srun <srun params> lengthy_command
then
    echo "Error occured"
fi

But after changing that line, the script will no longer run in an interactive session (outside SLURM). Is there any way to generalize the script so it can run under both conditions? I cannot see anything in the srun documentation that points in that direction.

like image 217
Poshi Avatar asked Jan 31 '26 18:01

Poshi


1 Answers

One option is to use a variable and set it to srun when in a job (for instance when $SLURM_JOBID is set), and to leave it empty otherwise.

if [ -n $SLURM_JOBID ] ;  
then 
LAUNCH=srun <srun params>
else
LAUNCH=
fi

if ! $LAUNCH lengthy_command
then
    echo "Error occured"
fi

This approach can be extended by setting LAUNCH to parallel <parallel options> when using GNU parallel, or to other commands when run in a cluster with another scheduler.

Another option is to create a Bash function named srun that encapsulates the logic (untested):

srun() {
if [ -n $SLURM_JOBID ] ;  
then 
command srun $@
else
$@
fi
}
like image 83
damienfrancois Avatar answered Feb 02 '26 12:02

damienfrancois