How to properly pass on an environment variable to Sun Grid Engine?

englebip picture englebip · Mar 15, 2012 · Viewed 7.4k times · Source

I'm trying to submit a (series of) jobs to SGE (FWIW, it's a sequence of Gromacs molecular dynamics simulations), in which all the jobs are identical except for a suffix, such as input01, input02, etc. I wrote the commands to run in a way that the suffix is properly handled by the sequence of commands.

However, I can't find a way to get the exec environment to receive that variable. According to the qsub man page, -v var should do it.

$ export i=19
$ export | grep ' i='
declare -x i="19"
$ env | grep '^i='
i=19

Then, I submit the following script (run.sh) to see if it's received:

if [ "x" == "x$i" ]; then
    echo "ERROR: \$i not set"
else
    echo "SUCCESS: \$i is set"
fi

I submit the job as follows (in the same session as the export command above):

$ qsub -N "test_env" -cwd -v i run.sh
Your job 4606 ("test_env") has been submitted

The error stream is empty, and the output stream has:

$ cat test_env.o4606 
ERROR: $i not set

I also tried the following commands, unsuccessfully:

$ qsub -N "test_env" -cwd -v i -V run.sh
$ qsub -N "test_env" -cwd -V run.sh
$ qsub -N "test_env" -cwd -v i=19 -V run.sh
$ qsub -N "test_env" -cwd -v i=19 run.sh

If I add a line i=19 to the beginning of run.sh, then the output is:

$ cat test_env.o4613
SUCCESS: $i is set as 19

I'm now considering generating a single file per job, which will essentially be the same but will have an i=xx line as the first. It doesn't look very much practical, but it would be a solution.

Would there be a better solution?

Answer

Jorge Leitao picture Jorge Leitao · Jan 23, 2013

What I've been always doing is the following:

##send.sh
export a=10
qsub ./run.sh

and the script run.sh:

##run.sh
#$ -V
echo $a

when I call send.sh, the .o has an output of 10.