How to wait for the completion of the system command

I am converting a CSV XLS 2 file with a system command in Ruby.

After the conversion, I process the CSV files, but the conversion is still performed when the program wants to process the files, so they are missing at this time.

Can someone tell me if it is possible to let Ruby wait for the right amount of time to complete the system command?

Now I am using:

sleep 20 

but if it takes more time once, this is not correct, of course.

What am I doing specifically:

 #Call on the program to convert xls command = "C:/Development/Tools/xls2csv/xls2csv.exe C:/TDLINK/file1.xls" system(command) do_stuff def do_stuff #This is where i use file1.csv, however, it isn't here yet end 
+6
ruby
source share
2 answers

Ruby system("...") method is synchronous; that is, he waits for the command that he calls to return the exit code, and system returns true if the command exited with status 0 and false if it exited with non-0 status. Ruby backticks return commmand output:

 a = `ls` 

sets a to the list box of the current working directory.

So, it seems that xls2csv.exe returns the exit code before it finishes what it should do. Perhaps this is a problem with Windows. Thus, it looks like you will have to loop until the file exists:

 until File.exist?("file1.csv") sleep 1 end 
+14
source share

Try using streams:

 command = Thread.new do system('ruby programm.rb') # long-long programm end command.join # main programm waiting for thread puts "command complete" 
+1
source share

All Articles