How to fix /usr/bin/env agrument processing?

Tag: linux , cluster-computing Author: ultra2007 Date: 2012-11-21

I've run into a weird problem with /usr/bin/env... I designed a simple script to show the problem. The script is in Ruby, but the same happens with a similar script in Python. Here is the script:

#!/usr/bin/env ruby


And another one without /usr/bin/env:



As you see it should just print script arguments. An it works flawlessly on the head node:

[[email protected] test]$ which ruby
[[email protected] test]$ ./script.no_usr_bin_env.rb 1 2 3
["1", "2", "3"]
[[email protected] test]$ ./script.usr_bin_env.rb 1 2 3
["1", "2", "3"]

But when running on computing node, it gets stuck:

[[email protected] test]$ qsub -d $(pwd) -I
qsub: waiting for job 176427.scyld.localdomain to start
qsub: job 176427.scyld.localdomain ready

-bash-3.2$ ./script.no_usr_bin_env.rb 1 2 3
["1", "2", "3"]
-bash-3.2$ ./script.usr_bin_env.rb 1 2 3

/usr/bin/env are exactly the same on both machines:

[[email protected] test]$ md5sum /usr/bin/env
7ada476000967f2e4cca2bc669045479  /usr/bin/env
[[email protected] test]$ qsub -I -d $(pwd)
qsub: waiting for job 176428.scyld.localdomain to start
qsub: job 176428.scyld.localdomain ready

-bash-3.2$ md5sum /usr/bin/env
7ada476000967f2e4cca2bc669045479  /usr/bin/env

I know that argument processing with /usr/bin/env can be tricky. But we have a lot of software using this and we cannot just fix them all. Is there anything I can do to fix this?

Best Answer

#!/usr/bin/env ruby causes the script to be executed be first ruby executable in $PATH. Your $PATH could be different on the compute node, most likely because of the way the environment is set up by qsub.

A quick and dirty fix would be:

PATH=/data/software/ruby-1.9.2-p180/bin:$PATH ./script.usr_bin_env.rb 1 2 3

but you should find a cleaner way to ensure that $PATH is set correctly before your Ruby scripts are executed.

For more detailed information, try which ruby and echo $PATH on the compute node, and make sure that the ruby command actually works.

(For more discussion on #!/usr/bin/env ... vs. #!.../ruby, see my answer to this question).