0

I am developing a ruby framework to run different jobs and one of the things that I need to do is to know when these jobs have ended in order to used their outputs and organize everything. I have been using it with no problem but some colegues are starting to use it in different system and something really odd is happening. What I do is run the commands using

i,o,e,t = Open3.popen3(job.get_cmd)
p = t.pid

and later I check if the job has ended like this:

begin
   Process.getpgid(p)
rescue Errno::ESRCH
   # The process ended
end

It works perfectly in the system I am running (Scientifi linux 6) but when a friend of mine started running on Ubuntu 14.04 (using ruby 1.9.3p484) and the command is a concatenation of commands such as cmd1 && cmd2 && cmd3 each command is run at the same time by the system, not one after the other, and the pid returned by t.pid is neither of the pids of the different processes being run.

I modified the code and instead of running the concatenation of cammands it creates a script with all the command inside the command called from popen3 is just Open3.popen3("./script.sh") but the behaviour is the same... All the commands are run at the same time and the pid that ruby knows is not any of the processes pid...

I am not sure if this is something ruby related but since running that script.sh by hand behaves as expected, running one command after the other, it seems that either ruby is not launching the process accordingly or the system is not reading the process as it should. Do you know what might be happening?

Thanks a lot!

EDIT: The command being run looks like this ./myFit.exe h vlq.config &> output_h.txt && ./myFit.exe d vlq.config &> output_d.txt && ./myFit.exe p vlq.config &> output_p.txt

This command, if run by hand and not inside the ruby script runs perfectly, exactly this command. When run from the ruby script it runs at the same time all the myFit.exe executions (but I want them to be run withh && becasue I want them running if the previous works fine). Myfit.exe is a tool which makes a fit, is not a system command. Again, this command, if run by hand runs perfeclty.

Juanpe Araque
  • 579
  • 1
  • 4
  • 16
  • Are there different shells running? – devanand Mar 09 '16 at 10:46
  • I am not sure... might it be that ruby is sending the job with a different shell if there were? Is there a way to tell it how to run it? The system uses bash by default... – Juanpe Araque Mar 09 '16 at 10:58
  • 1
    If you are running `Open3.popen3("./script.sh")` then it depends which shell is installed. Therefor the result is maybe different. – devanand Mar 09 '16 at 11:00
  • But the first version of the code run only the command, not a script, i.e. `job.get_cmd = cmd1 && cmd2 && cm3` so in principle it should run fine isn't it? And if one does by hand `./script.sh` then it runs fine as expected. – Juanpe Araque Mar 09 '16 at 11:01
  • Do you really mean that you pass to popen3 a command, which, as a **string**, is 'cmd1 && cmd2 && cmd3' ? In this case, you should research first, which shell is actually executing the commands, because in all shells I know (bash, zsh, ksh, csh), the `&&` explicitly **serializes** the commands. What happens, if you invoke the shell explicitly? Please post also more code, so that we see what `job.get_cmd` really returns. – user1934428 Mar 09 '16 at 11:06
  • Yes, I put that command as a string, I updated the question with the actual command which is being passed to popen3. I know that it should serialize the commands and that is why I cannot understand what is happening in this system... Thanks a lot – Juanpe Araque Mar 09 '16 at 11:16
  • I have tried running a popen3 process that only prints the $SHELL variable when running the process to see if for some reason it was running something different than bash and the output said /bin/bash so... no clue... this is completely puzzling me... – Juanpe Araque Mar 09 '16 at 11:37
  • For some reason, which I haven't understood yet, the problem was the weirdest thing I have ever seen but I found the reason why it wasn't working. When launching a command with popen3 it seemed like the process did not know any shell (i.e. I tried to run just `echo $SHELL` and it turned out to be empty) but the script was read and each of the commands inside the file were launched using bash, the only shell installed in the system. The way of fixing it was with @user1934428, explicitly telling to use bash with the command `/bin/bash script.sh` then it worked. – Juanpe Araque Mar 10 '16 at 14:59
  • Well, the content of the SHELL variable doesn't mean anything, but I notice that you have a program which has the extension ".exe". This is very rare on Unix systems. Could it be that you are using Cygwin on Windows? – user1934428 Mar 11 '16 at 08:05
  • Ah, sorry, I just noticed: You explicitly say that you are on Linux. BTW, how did you verify, that the programs were running in parallel, not sequentially? – user1934428 Mar 11 '16 at 08:06

0 Answers0