parameter for shell scripts that is started with qsub
how can I parametrize a shell script that is executed on a grid (started with qsub) ? I have a shell script, where I use getopts to read the parameters.
When I start (qsub script.sh -r firstparam -s secondparam ..) this working script with qsub I receive error messages,
qsub: invalid option -- s
qsub: illegal -r value
as qsub thinks the parameter are for itself. Yet I have not found any solution.
Thanks
Using the qsub -v option is the proper way:
qsub -v par_name=par_value[,par_name=par_value...] script.sh
par_name can be used as variable in the shell script.
In addition to volk's answer, in order to reference the variables in the list (designated by -v) you simply use the name you define in your call. So, say you made a call to qsub as follows
qsub -v foo='qux' myRunScript.sh
Then myRunScript.sh
could look something like this:
#!/bin/bash
#PBS -l nodes=1:ppn=16,walltime=0:00:59
#PBS -l mem=62000mb
#PBS -m abe
bar=${foo}
echo "${bar}"
Where the output would be
qux
Hope this helps!
I just figured out how to solve it: just print the commands of the shell scrip with echo and pipe the result to qsub:
echo "./script.sh var1=13 var2=24" | qsub
There is a better way...
I'm really surprised at how long this question has gone without a good answer. It may be that the specific version of qsub
wasn't specified. qsub
exists in at least Torque and also Sun Grid Engine, maybe other schedulers. So, it's important to know which you're using. I'll talk about a few here:
TORQUE: qsub -F <arguments> command
man page
Here's an example of how I normally use it. Starting with this example script which just echoes any arguments passed to it:
$ cat testArgs.pbs
#!/usr/bin/env bash
echo $@
I would submit the job like this:
$ qsub -F "--here are the --args" testArgs.pbs
3883919.pnap-mgt1.cm.cluster
And this is what the output file looks like after it runs:
$ cat testArgs.pbs.o3883919
--here are the --args
Sun Grid Engine: qsub command [ command_args ]
man page
You just add the arguments after the command, same as you would when executing in the shell. I don't have SGE running anywhere, so no example for this one. But it's the same with Slurm, which is below
Slurm: sbatch command [ command_args ]
man page
Here I submit the same script I used with the Torque example above:
$ sbatch testArgs.sh what the heck
Submitted batch job 104331
And the results:
$ cat slurm-104331.out
what the heck
Exporting environment variables != passing arguments
Exporting environment variables is very different from passing arguments to a command.
Here is a good discussion on the differences.
The qsub
answers above all recommend -v
. To be clear, -v
exports environment variables, -F
passes arguments to the command.
I generally prefer to parameterize my scripts by allowing for arguments. In fact, I would say it's much more common to use scripts like this process_data.sh --threads 8
than doing something like export THREADS=8; process_data.sh
.