parameter for shell scripts that is started with qsub
Asked Answered
P

4

49

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

Phelloderm answered 17/8, 2010 at 15:22 Comment(3)
I just figured out how to solve it: just print the commands of the shell scrip with echo and pipe the result to qsub: ./script.sh | qsubPhelloderm
You should post your solution as an answer. Then come back and mark it accepted. By the way, did you try quoting the argument to qsub? qsub 'script.sh -r firstparam -s secondparam' I have no idea if that works in this case.Shah
I've seen more than one command named qsub; are you referring to this one?Fugate
P
22

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

Phelloderm answered 1/9, 2010 at 13:25 Comment(6)
your answer needs to be more explicit - come up with an exampleAmbidexterity
It seems my problem, is similar. How do I need to change my bash script? Can you give an example.Ooze
The problem in this answer is that the job name is always STDIN (as appears in qstat -a), at least for my caseTagliatelle
@Tagliatelle just use echo "./script.sh var1=13 var2=24" | qsub -N myjobnameClare
I'm afraid this just runs script.sh on the node where qsub is executed, not on the cluster.Hereby
In my qsub version (probably Univa Grid Engine, this is written in man, but no --version option exists) - there is nothing written about reading input. I'm afraid that's not portable and reliable. Anyway, glad that it works for you.Peabody
L
45

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.

Ladin answered 19/1, 2011 at 10:7 Comment(3)
Thanks for your suggestion! In my opinion the problem with this approach is the difference of the parameter handling for the use of one application as standalone or distributed app. Therefore I prefer the style described above.Phelloderm
What if I don't care about the names of variables (because in my script I use just $1, $2 and so on)? To be more precise, all I want is just to pass arguments to the script.Hilliard
I also don't care about the names of the variables - I prefer the script to parse the options as it normally would when running directly from a terminal, even when run with qsub.Mulvihill
S
38

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!

Simply answered 12/6, 2013 at 15:5 Comment(3)
@_jhrf I think you wanted $barGuertin
You're right. I've looked at this answer many times and not seen that error! Thanks.Simply
@Guertin and @Simply because you specifically gave bash, it does not matter in this case. Though the ` = ` (with spaces is a syntax problem...). See this excellent answer for why $bar and ${bar} both work here :)Jeannajeanne
P
22

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

Phelloderm answered 1/9, 2010 at 13:25 Comment(6)
your answer needs to be more explicit - come up with an exampleAmbidexterity
It seems my problem, is similar. How do I need to change my bash script? Can you give an example.Ooze
The problem in this answer is that the job name is always STDIN (as appears in qstat -a), at least for my caseTagliatelle
@Tagliatelle just use echo "./script.sh var1=13 var2=24" | qsub -N myjobnameClare
I'm afraid this just runs script.sh on the node where qsub is executed, not on the cluster.Hereby
In my qsub version (probably Univa Grid Engine, this is written in man, but no --version option exists) - there is nothing written about reading input. I'm afraid that's not portable and reliable. Anyway, glad that it works for you.Peabody
C
20

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.

Chrissychrist answered 20/9, 2017 at 18:6 Comment(2)
Thanks! In my environment, man qsub says there is no -F option, but I tried anyway and it works! Probably this option was added recently. But it is the right answer to this question.Encephalic
thanks! I am using qsub (Sun Grid Engine) and it works! note that you don't need to add "command" as a key word. qsub script.sge argumentsPassim

© 2022 - 2024 — McMap. All rights reserved.