目前我面临的一个问题是:我们需要从网页控制后台运行的linux脚本。例如我们可以通过网页上的按钮开始/停止“脚本”。
另一个很好的例子是:
我是网站开发专家,非常熟悉javascript(setTimeout刷新进度),ruby on rails。
非常感谢。
答案 0 :(得分:0)
如果是linux系统,那么你可以使用
system(path_to_script.sh)
或
`path_to_script.sh`
控制器中的
答案 1 :(得分:0)
感谢@xvidun,我找到了解决方案:使用god。
关键过程是:如何使非守护进程作为守护进程启动。 “nohup ......&”不适合我。所以我试过上帝,这很棒!
在我的情况下,我想要运行的过程是:
$ cd /sg552/workspace/m-video-fetcher
$ nohup ruby script/start_fetch.rb &
这是我完成这项工作的方式:
step1:创建一个神配置文件:
# file name: fetcher.god
RAILS_ROOT = '/sg552/workspace/m-video-fetcher'
God.watch do |w|
w.name = 'fetcher'
w.dir = RAILS_ROOT
w.start = "ruby script/start_fetch.rb"
w.log = "#{RAILS_ROOT}/fetcher_stdout.log"
w.keepalive
end
第二步:
$ god start -c fetcher.god
步骤3:
# in the view, give users the interface to restart this job.
<a href='/settings/restart_fetch_for_all_plans'>restart</A>
# in the controller:
def restart_fetch_for_all_plans
result = `god stop fetcher && god start fetcher`
redirect_to :back, :notice => "fetcher restarted, #{result}"
end