How to stop Daemon server in Rails?

I run my rails application using the following

$script/server -d webrick 

on my Ubuntu system, above the command, start the webrick server in the background. I can kill a process using the kill command

  $kill pid 

Does the rails provide any command to stop the daemon server from starting in the background?

as provided by the rails for starting the server, thanks.

EDIT . When do I need to start the daemon server? Any real-time script will help. Thanks.

+74
ruby-on-rails ubuntu
Jul 22. '09 at 9:24
source share
15 answers

How about a rake challenge?

 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 

start with stop rake or stop sudo rake

+32
May 25 '12 at 2:06
source share

if this can be useful, in linux you can find which process uses the port (in this case 3000) you can use:

lsof -i: 3000

it will also return pid

+81
Jan 24 2018-11-12T00:
source share

As Ryan said:

the pid you want is in tmp / pids /

Perhaps server.pid is the file you want.

You can run kill -9 $(cat tmp/pids/server.pid) to tear down the daemon server.

+36
Feb 02 '10 at 22:32
source share

The daemon server process ID is stored in your tmp / pids / application directory. You can use your standard kill process_id with the information you find there.

+18
Aug 14 '09 at 23:35
source share

The only right way to kill the default Ruby on Rails server (which is WEBrick):

 kill -INT $(cat tmp/pids/server.pid) 

If you are using Mongrel, this is enough:

 kill $(cat tmp/pids/server.pid) 

Use kill -9 if your daemon was hanging. Remember the kill -9 values ​​- if the data stored in the Active Record caches was not flushed to disk, you will lose your data. (As I recently did)

+16
Apr 4 2018-12-12T00:
source share

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

 $ lsof -wni tcp:3000 

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

 $ kill -9 <PID> 
+14
03 Oct '12 at 12:10
source share

pguardiario defeated me, although its implementation is a bit dangerous because SIGKILL used instead of SIGKILL . Here is the rake task that I am trying to import into my development projects:

Lib / tasks / stopserver.rake

 desc 'stop server' task :stopserver do pid_file = 'tmp/pids/server.pid' if File.file?(pid_file) print "Shutting down WEBrick\n" pid = File.read(pid_file).to_i Process.kill "INT", pid end File.file?(pid_file) && File.delete(pid_file) end 

This causes an interrupt for the server if and only if the pidfile exists. It does not throw unsightly errors if the server is not running, and it notifies you if it actually closes the server.

If you notice that the server does not want to close this task, add the following line after the line Process.kill "INT" and try switching to the kernel with a fixed error.

 Process.kill "CONT", pid 

(hat tip: jackr )

+6
Jun 13 2018-12-12T00:
source share

Run this command:

 locate tmp/pids/server.pid 

output: Full path to this file. Check the name of the project directory to find the corresponding file if several files are listed.

Then run this command:

 rm -rf [complete path of tmp/pids/server.pid file] 
+6
Sep 25 '14 at 2:05
source share

A Ruby ticket, http://bugs.ruby-lang.org/issues/4777 , indicates a kernel error (Linux). They give a job (essentially equivalent to Ctrl-C / Ctrl-Z), for use if you demonized the server:

  • kill -INT cat tmp/pids/server.pid
  • kill -CONT cat tmp/pids/server.pid

This, apparently, leads to the processing of the original signal INT, perhaps it allows you to reset the data, etc.

+5
Jun 12 2018-12-12T00:
source share

Here I leave the bash function, which, if you inserted .bashrc or .zshrc in you, will be fused, 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 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; } 

Additional information in the blog post I wrote about this.

+4
May 03 '14 at 10:33
source share

I do not think this will happen if you use -d. I will just kill the process.

In the future, just open another terminal window and use the command without -d, it provides really useful debug output.

If this is production, use something like a passenger or thin to make it easy to stop processes or restart servers.

+3
Jul 22 '09 at 12:06
source share
 one-liner: kill -INT `ps -e |  grep ruby ​​|  awk '{print $ 1}' '

ps -e lists all processes in the system
grep ruby searches for this result for a ruby ​​process
awk passes the first argument of this output (pid) to kill -INT .


Try using echo instead of kill if you just want to see the PID.

+3
Feb 24 '13 at 15:17
source share

if the kill process does not work, then delete the server.pid file from MyRailsApp / tmp / pids /

+2
Mar 21 '14 at 5:03
source share

I came here because I tried (unsuccessfully) to stop with normal killing and thought that I would do something wrong.

Killing -9 is the only sure way to stop ruby ​​on a rails server? What kind!? Do you know the consequences of this? It could be a disaster ...

+1
Jan 19 '12 at 15:40
source share

You can start your server in the background by adding -d to your command. For example:

 puma -d 

To stop it, just kill any process running on port 3000:

 kill $(cat tmp/pids/server.pid) 
+1
Aug 23 '18 at 15:33
source share



All Articles