Ruby 如何阻止sinatra跑步?

Ruby 如何阻止sinatra跑步?,ruby,sinatra,Ruby,Sinatra,如果ruby myapp.rb在localhost:4567启动sinatra预览,我如何通过编程停止/停止/杀死它?终端命令(Ctrl-C除外)或Rake任务也可以 我需要将其合并到Rake任务或终端中。在myapp.rb中,在sinatra启动之前添加: puts "This is process #{Process.pid}" 当您想杀死它时,请在壳中执行以下操作: kill <pid> 在OS X中,从命令行(Terminal.app或DTerm)只需输入: $ kill

如果
ruby myapp.rb
在localhost:4567启动sinatra预览,我如何通过编程停止/停止/杀死它?终端命令(Ctrl-C除外)或Rake任务也可以


我需要将其合并到Rake任务或终端中。

在myapp.rb中,在sinatra启动之前添加:

puts "This is process #{Process.pid}"
当您想杀死它时,请在壳中执行以下操作:

kill <pid>

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

$ killall ruby
每个ruby进程都将停止。西纳特拉也是

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

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

最简单的方法是:

kill #{Process.pid}

要以简单、可重复的方式实现这一点,有几种方法

  • 在启动Sinatra服务器时记录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 $!)
    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
    
  • 您可以使用临时文件,例如
    MY\u SINATRA\u 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)
    
    #运行Sinatra服务器并将其发送到后台(使用&)
    ruby my_sinatra_server.rb&
    #记录最后一个后台进程的PID(使用$!)
    echo$!>my_sinatra_server.pid
    #现在开始做你的事吧。。。
    #完成后,杀死sinatra服务器(从同一个shell)
    kill$(

  • 西纳特拉从不跑步。她的靴子是用来走路的…谢谢,特别是我正在使用的最后一个代码。我需要将
    Process.kill'TERM',File.read('myapp.pid')转换为int。转换为\u I
    # 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
    
    # 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)