如何阻止 sinatra 运行?
如果 ruby myapp.rb 在 localhost:4567 启动 sinatra 预览,我如何以编程方式停止/停止/终止它?终端命令(Ctrl-C 除外)或 Rake 任务就可以了。
我需要将其合并到 Rake 任务或终端中。
If ruby myapp.rb
starts sinatra previewing at localhost:4567, how can I programatically stop/halt/kill it? Terminal command (other than Ctrl-C), or Rake tasks would be fine.
I need to incorporate this into a Rake task or terminal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 myapp.rb 中,在 sinatra 启动之前添加以下内容:
当您想要终止它时,请在 shell 中执行此操作:
其中
是 myapp.rb 输出的数字。如果你想在 ruby 中执行此操作:这两个命令都会让 sinatra 运行它的退出例程。如果您不想每次都输入 pid,请让 myapp.rb 打开一个文件并将其 pid 放入其中。然后,当您想停止它时,读取该文件并使用它。例子:
In myapp.rb, add this before sinatra starts:
When you want to kill it, do this in a shell:
Where
<pid>
is the number outputted by myapp.rb. If you want to do it in ruby:Both of these will let sinatra run it's exit routine. If you don't want to type in the pid every time, have myapp.rb open a file and put it's pid in it. Then when you want to stop it, read the file and use that. Example:
在 OS X 中,从命令行(Terminal.app 或 DTerm)只需输入:
每个 ruby 进程都会停止。西纳特拉也是。
在 Linux(和其他 UNIX)中,您可以:
In OS X, from the command line (Terminal.app, or DTerm) just enter:
every ruby process will stop. Sinatra too.
In Linux (and other UNIXes), you can:
最简单的方法是:
The simples way to do that:
为了以简单、可重复的方式做到这一点,有几种方法。
在启动 Sinatra 服务器时记录 PID,例如
您可以使用临时文件,例如
my_sinatra_server,而不是使用环境变量 (
$MY_SINATRA_SERVER
) .pidTo do this in a simple repeatable way, there's a few methods.
Record the PID as you start your Sinatra server, e.g.
Instead of using an env variable (
$MY_SINATRA_SERVER
) you can use a temporary file e.g.my_sinatra_server.pid