如何阻止 sinatra 运行?

发布于 2024-09-08 16:42:52 字数 134 浏览 3 评论 0原文

如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十二 2024-09-15 16:42:52

在 myapp.rb 中,在 sinatra 启动之前添加以下内容:

puts "This is process #{Process.pid}"

当您想要终止它时,请在 shell 中执行此操作:

kill <pid>

其中 是 myapp.rb 输出的数字。如果你想在 ruby​​ 中执行此操作:

Process.kill 'TERM', <pid>

这两个命令都会让 sinatra 运行它的退出例程。如果您不想每次都输入 pid,请让 myapp.rb 打开一个文件并将其 pid 放入其中。然后,当您想停止它时,读取该文件并使用它。例子:

# myapp.rb:
File.open('myapp.pid', 'w') {|f| f.write Process.pid }

# shell:
kill `cat myapp.pid`

# ruby:
Process.kill 'TERM', File.read('myapp.pid')

In myapp.rb, add this before sinatra starts:

puts "This is process #{Process.pid}"

When you want to kill it, do this in a shell:

kill <pid>

Where <pid> is the number outputted by myapp.rb. If you want to do it in ruby:

Process.kill 'TERM', <pid>

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:

# myapp.rb:
File.open('myapp.pid', 'w') {|f| f.write Process.pid }

# shell:
kill `cat myapp.pid`

# ruby:
Process.kill 'TERM', File.read('myapp.pid')
寂寞花火° 2024-09-15 16:42:52

在 OS X 中,从命令行(Terminal.app 或 DTerm)只需输入:

$ killall ruby

每个 ruby​​ 进程都会停止。西纳特拉也是。

在 Linux(和其他 UNIX)中,您可以:

$ ps aux | grep ruby
$ kill <ruby-process-id>

In OS X, from the command line (Terminal.app, or DTerm) just enter:

$ killall ruby

every ruby process will stop. Sinatra too.

In Linux (and other UNIXes), you can:

$ ps aux | grep ruby
$ kill <ruby-process-id>
只是一片海 2024-09-15 16:42:52

最简单的方法是:

kill #{Process.pid}

The simples way to do that:

kill #{Process.pid}
萌梦深 2024-09-15 16:42:52

为了以简单、可重复的方式做到这一点,有几种方法。

  1. 在启动 Sinatra 服务器时记录 PID,例如

    # 运行 Sinatra 服务器并将其发送到后台(使用 &)
    ruby my_sinatra_server.rb &
    
    # 记录最后一个后台进程的PID(使用$!)
    MY_SINATRA_SERVER_PID=$!
    
    # 现在继续做你的事情......
    
    # 完成后,终止 sinatra 服务器(从同一个 shell)
    杀死 $MY_SINATRA_SERVER_PID
    
  2. 您可以使用临时文件,例如 my_sinatra_server,而不是使用环境变量 ($MY_SINATRA_SERVER) .pid

    # 运行 Sinatra 服务器并将其发送到后台(使用 &)
    ruby my_sinatra_server.rb &
    
    # 记录最后一个后台进程的PID(使用$!)
    回声$! > my_sinatra_server.pid
    
    # 现在继续做你的事情......
    
    # 完成后,终止 sinatra 服务器(从同一个 shell)
    杀死 $(< my_sinatra_server.pid)
    

To do this in a simple repeatable way, there's a few methods.

  1. Record the PID as you start your Sinatra server, e.g.

    # Run the Sinatra server and send it to background (using &)
    ruby my_sinatra_server.rb &
    
    # Record the PID of the last background process (using $!)
    MY_SINATRA_SERVER_PID=$!
    
    # Now go ahead and do your stuff...
    
    # When finished, kill the sinatra server (from the same shell)
    kill $MY_SINATRA_SERVER_PID
    
  2. Instead of using an env variable ($MY_SINATRA_SERVER) you can use a temporary file e.g. my_sinatra_server.pid

    # Run the Sinatra server and send it to background (using &)
    ruby my_sinatra_server.rb &
    
    # Record the PID of the last background process (using $!)
    echo $! > my_sinatra_server.pid
    
    # Now go ahead and do your stuff...
    
    # When finished, kill the sinatra server (from the same shell)
    kill $(< my_sinatra_server.pid)
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文