0

I am using shell command to zip file in ruby. Then I am uploading the zipped file to server. when I use it in a loop like:

dump_files.each do |dump_file|
  Open3.popen3("zip #{zip_file}  #{dump_file}")
end

And upload, the last file in the dump_files array is not present in the uploaded zipfile but it present in the local file.

I think it happens because of the time delay to zip the file. How can I delay my ruby execution till the zip command execution complete?

3
  • 1
    The problem must be something else. There is nothing to delay, because when you run a shell command with backticks the shell command will not return to Ruby until it is complete. Try sleep 10 and you will see. Commented Aug 3, 2011 at 7:20
  • Also it looks suspect that you use double quotes around your command "zip ..". You should just run zip .... Commented Aug 3, 2011 at 7:23
  • sorry friends, previously I was using Open3.popen3() for executing the shell command. Now it works when I use `` instead of popen3(). thank you very much Commented Aug 3, 2011 at 7:43

2 Answers 2

3

Shouldn't that be:

`zip "#{zip_file}"  "#{dump_file}"`

(in other words, you're not zipping at all?)

Sign up to request clarification or add additional context in comments.

4 Comments

@Sayuj: You are actually trying to call command named "zip zipfile dumpfile", not a zip command with arguments. Remove your double quotes. @Denis: He doesn't even need doublequotes around #{} as backtick operator already does string interpolation.
yea. okay. my problem is the delay in zipping
@Sayuj - I think you must have some old file laying around, and you're just thinking it works, but in reality it's not. Check your commands manually in irb.
sorry friends, previously I was using Open3.popen3() for executing the shell command. Now it works when I use `` instead of popen3(). than you very much guys
0

Using `` instead of popen3 will fix the issue

replace

Open3.popen3("zip #{zip_file}  #{dump_file}")

with

`zip #{zip_file}  #{dump_file}`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.