找到你要的答案

Q:How to run a ruby shell script that shows some information, then asks for input and goes background?

Q:如何运行Ruby shell脚本显示一些信息,然后要求输入和去背景?

I have a ruby shell script that I want to run on background. However, before going to background I need to ask for an user input.

Is it possible? How can it be achieved?

Thanks in advance!

I have a ruby shell script that I want to run on background. However, before going to background I need to ask for an user input.

这可能吗?如何才能实现?

先谢谢了。

answer1: 回答1:

Here's a simple example:

def do_stuff(input)
  puts "Input received: #{input}"
  sleep 10
end

print "Enter input value: "
input = gets
pid = fork { do_stuff(input) }
puts "test.rb started, pid: #{pid}"
Process.detach pid

Note the use of fork to start a new process, and detach to allow the subprocess to continue running while the main process exits.

Some platforms (Windows) don't support fork.

这里有一个简单的例子:

def do_stuff(input)
  puts "Input received: #{input}"
  sleep 10
end

print "Enter input value: "
input = gets
pid = fork { do_stuff(input) }
puts "test.rb started, pid: #{pid}"
Process.detach pid

注意叉开始一个新的使用过程中,将允许子进程继续在主进程退出运行。

一些平台(窗口)不支持叉。

answer2: 回答2:

It's definitely possible, My understanding is that non-essential features are pushed to background jobs to alleviate some processing burden for the user experience. I'm doing something along these lines right now but not activated by the user.

You have to ask some questions first I think: Does the process give user feedback in real time (not a good use case for a background job)? How many jobs can one expect to be running simultaneously? How will it interfere or operate with production workers (like unicorn spawn)? What happens if the background jobs fail? Are they processing high priority information? These questions guided me to figure out what to leverage how/when to monitor and whether or not I want it triggered by user input vs on boot vs etc.

Anyway, I would suggest using: http://sidekiq.org/ , It seems to be winner amongst a lot of developers. I would probably implement it by accepting the parameters in the controller and instantiating the worker class (as you can see in sidekiqs documentation) inside a controller method. That worker class will run your ruby shell script probably with Runner like the question here? Runner in ruby on rails

You can even trigger them off a model (like emailing after user creation) or what have you.

Another frequently used gem is https://github.com/collectiveidea/delayed_job . I am personally using https://github.com/javan/whenever to activate rake tasks (as cron jobs) when deploying to production. Thats doesn't seem very relevant to you though.

For future questions of this broad nature you should provide some more information on the jobs requirements, such like answering some of the questions in the second paragraph. What are you using for? Do the parameters trigger different scripts? What rails version are you using? Is it used on production or development?

这是绝对可能的,我的理解是,非必需的功能被推到后台作业,以减轻一些处理负担的用户体验。我正在做的事情沿着这些线现在,但没有激活用户。

你必须先问一些问题:我认为:这个过程给用户反馈实时(不是一个很好的用例的背景工作)?有多少工作可以期望同时运行?它将如何干扰或操作与生产工人(如麒麟产卵)?如果后台作业失败会发生什么?他们处理高优先级的信息吗?这些问题引导我找出什么来利用如何/何时来监视和是否我想要它由用户输入触发VS开机VS等

无论如何,我会建议使用:http://sidekiq.org/,它似乎是赢家在很多开发商。我会实现它接受参数的控制器和以工人阶级(你可以看到sidekiqs文件)在控制器的方法。工人类将运行您的Ruby shell脚本可能与亚军像这里的问题?轨道上红宝石的转轮

你甚至可以触发了他们一个模型(如电子邮件在用户创建)或者你有什么。

另一个常用的宝石https://github.com/collectiveidea/delayed_job。我个人使用的https://github.com/javan/whenever激活前任务(如cron作业)在部署到生产。这似乎不太相关,虽然你。

对于未来的问题,这种广泛的性质,你应该提供更多的信息,对工作的要求,如回答一些问题的第二段。你用的是什么?参数触发不同的脚本吗?您使用的Rails版本是什么?它是用于生产或开发?

ruby  ubuntu  background-process