How to stop a Daemon Server in Rails?

前端 未结 15 564
灰色年华
灰色年华 2020-12-12 09:52

I am running my rails application using the following

  $script/server -d webrick 

on my Ubuntu system , above command run the webrick s

相关标签:
15条回答
  • 2020-12-12 10:26

    Run this command:

    locate tmp/pids/server.pid
    

    output: Complete path of this file. Check your project directory name to find your concerned file if multiple files are shown in list.

    Then run this command:

    rm -rf [complete path of tmp/pids/server.pid file]
    
    0 讨论(0)
  • 2020-12-12 10:27

    Here I leave a bash function which, if pasted in you .bashrc or .zshrc will alloy you do things like:

    rails start # To start the server in development environment
    rails start production # To start the server in production environment
    rails stop # To stop the server
    rails stop -9 # To stop the server sending -9 kill signal
    rails restart # To restart the server in development environment
    rails restart production # To restart the server in production environment
    rails whatever # Will send the call to original rails command
    

    Here it is the function:

    function rails() {
      if [ "$1" = "start" ]; then
         if [ "$2" = "" ]; then
            RENV="development"
         else
            RENV="$2"
         fi
         rails server -d -e "$RENV"
         return 0
      elif [ "$1" = "stop" ]; then
         if [ -f tmp/pids/server.pid ]; then
            kill $2 $(cat tmp/pids/server.pid)
            return 0
         else
            echo "It seems there is no server running or you are not in a rails project root directory"
            return 1
         fi
      elif [ "$1" = "restart" ]; then
         rails stop && rails start $2
      else
         command rails $@
      fi;
    }
    

    More information in the blog post I wrote about it.

    0 讨论(0)
  • 2020-12-12 10:30

    Like Ryan said:

    the pid you want is in tmp/pids/

    probably server.pid is the file you want.

    You should be able to run kill -9 $(cat tmp/pids/server.pid) to bring down a daemonized server.

    0 讨论(0)
  • 2020-12-12 10:31

    How about a rake task?

    desc 'stop rails'
    task :stop do
        pid_file = 'tmp/pids/server.pid'
        pid = File.read(pid_file).to_i
        Process.kill 9, pid
        File.delete pid_file
    end
    

    run with rake stop or sudo rake stop

    0 讨论(0)
  • 2020-12-12 10:31

    In your terminal to find out the process id (PID):

    $ lsof -wni tcp:3000
    

    Then, use the number in the PID column to kill the process:

    $ kill -9 <PID>
    
    0 讨论(0)
  • 2020-12-12 10:34

    I came here because I were trying to (unsuccesfully) stop with a normal kill, and thought I'd being doing something wrong.

    A kill -9 is the only sure way to stop a ruby on rails server? What!? Do you know the implications of this? Can be a disaster...

    0 讨论(0)
提交回复
热议问题