Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for system command to end

Tags:

ruby

I'm converting an XLS 2 CSV file with a system command in Ruby.

After the conversion I'm processing the CSV files, but the conversion is still running when the program wants to process the files, so at that time they are non-existent.

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

Right now I'm using:

sleep 20

but if it will take longer once, it isn't right of course.

What I do specifically is this:

#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
like image 838
Ignace Avatar asked May 26 '10 06:05

Ignace


1 Answers

Ruby's system("...") method is synchronous; i.e. it waits for the command it calls to return an exit code and system returns true if the command exited with a 0 status and false if it exited with a non-0 status. Ruby's backticks return the output of the commmand:

a = `ls`

will set a to a string with a listing of the current working directory.

So it appears that xls2csv.exe is returning an exit code before it finishes what it's supposed to do. Maybe this is a Windows issue. So it looks like you're going to have to loop until the file exists:

until File.exist?("file1.csv")
  sleep 1
end
like image 82
Aaron Gibralter Avatar answered Oct 20 '22 11:10

Aaron Gibralter