I'm trying to create a function that serves as a drop-in replacement for the SLURM's srun
command. The need for this wrapper function is that I want to codify a script using srun
when being started under SLURM control, but still being able to run the script without SLURM.
So far, I have this function:
srun_wrap() {
if [ -z "$SLURM_JOB_ID" ]
then
# Not running under SLURM so start the code without srun
"${@:2}"
else
# A SLURM job ID found, so use srun
srun ${@:1:1} "${@:2}"
fi
}
This allows me to transform a line like
srun --job-name listing ls
to a line like
srun_wrapper "--job-name listing" ls
Quite close to a drop-in, but not yet.
The rational is:
- Check variable
$SLURM_JOB_ID
for some value- If there is no value in the variable, it means that we are not under SLURM and we should run the command without srun. The brace expansion ignores the first argument (the
srun
parameters) and run the rest of the command line. - If there is some value, it means that we are in a SLURM allocation, so use
srun
. The command line is formed withsrun
, the first parameter unquoted to allowsrun
to identify properly the parameters and finally, the real command line, properly quoted.
- If there is no value in the variable, it means that we are not under SLURM and we should run the command without srun. The brace expansion ignores the first argument (the
This approach still have two drawbacks:
- The
srun
parameters in the brace expansion have to be unquoted, otherwise they are not properly parsed bysrun
. - The
srun
parameters when making the call have to be passed in quotes to be considered as a single parameter. - I'm forced to always pass a parameter to the wrapper, even an empty one. A
srun ls
will be translated tosrun_wrapper "" ls
.
Any ideas on how to overcome those three drawbacks? How to quote the brace expansion, how to avoid quoting the srun
parameters and how to avoid the need of an empty parameter?