在ruby中执行shell命令,使用正确的参数转义 [英] Executing shell command in background from ruby with proper argument escaping

查看:328
本文介绍了在ruby中执行shell命令,使用正确的参数转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如果我写了 system(rake,send_mails,params [ :subject])然后我没有地方的重定向和& 标志。如果我没有为主题参数转义。



我该如何解决? / p>

解决方案

在Ruby 1.9中,尝试 Process.spawn

 #产生一个新的过程,运行rake命令
pid = Process.spawn({subject=> params [:subject]},
rake,send_mails,
:out => dev / null',:err =>'dev / null')

#分离生成的进程
Process.detach pid
pre>

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?

解决方案

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

这篇关于在ruby中执行shell命令,使用正确的参数转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆