开发者

How to wait for system command to end

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.e开发者_StackOverflow社区xe 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


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


Try to use threads:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜