Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing shell command in background from ruby with proper argument escaping

I have to run a command in the background but I want to have proper escaping for its parameter.

system("rake send_mails subject='#{params[:subject]}' 2> /dev/null 1> /dev/null &");

If I write system("rake", "send_mails", params[:subject]) then I don't have "place" for redirections and the & sign. If I don't I do not have escaping for the subject parameter.

How can I resolve this?

like image 654
Notinlist Avatar asked May 10 '11 11:05

Notinlist


1 Answers

In Ruby 1.9, try Process.spawn:

# Spawn a new process and run the rake command
pid = Process.spawn({"subject" => params[:subject]},
                    "rake", "send_mails",
                    :out => 'dev/null', :err => 'dev/null')

# Detach the spawned process
Process.detach pid
like image 105
Lars Haugseth Avatar answered Nov 18 '22 03:11

Lars Haugseth