使用适当的参数转义从 ruby​​ 在后台执行 shell 命令 [英] Executing shell command in background from ruby with proper argument escaping

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

问题描述

我必须在后台运行一个命令,但我想对其参数进行适当的转义.

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 &");

如果我写 system("rake", "send_mails", params[:subject]) 那么我没有重定向的地方"和 & 签到.如果我没有,我没有 subject 参数的转义.

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?

推荐答案

在 Ruby 1.9 中,尝试 Process.spawn:

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天全站免登陆