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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With