Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will "system()" call in Ruby wait until it finishes?

I'm new to ruby on rails. I want to call a system command to analyze an uploaded file in my Rails application. Something as follows:

after_save :analyse  #post processing call
def analyse
    command = "./c_executable " + Rails.root.to_s + "/output_csv_file"
    system(command)
    if FileTest.exists?(Rails.root.to_s + "/output_csv_file")
       parse_csv
    end
end

It runs fine on my local machine, but for some reason the function "parse_csv" won't get called on the server(dreamhost). However, I manually call the c_executable system command on the server and it outputs the csv file without a problem. Could anyone tell me what might be causing the problem here? I was thinking the system call takes some time to finish on the server. If that's the case, I'm wondering if there is a way to tell rails to wait until the system() call finishes execution. Thanks in advance!

like image 222
saurb Avatar asked Feb 16 '26 02:02

saurb


1 Answers

The system call should block until the command inside is finished. It is possible that the file is not being created as you intended which might preclude that part of your app from running.

You might want to use a different way of constructing your path to be sure you're getting it right:

csv_path = File.expand_path('output_csv_file', Rails.root)

unless (system('./c_executable', csv_path))
  # Could't execute system command for some reason.
end

if (File.exists?(csv_path))
  # ...
end

When making system calls it's generally a good idea to specify the full path to the executable as your application PATH may be different from what you expect.

like image 57
tadman Avatar answered Feb 17 '26 15:02

tadman