run and stop rake task from ruby file - ruby-on-rails

I already know that running a rake task from .rb file can be done by running:
system "/usr/bin/rake #{task} #{args.join(' ')} > #{Rails.root}/log/rake.log"
But how to stop a currently running rake task from ruby file?
Thanks!

Perhaps you could do a
ps_aux = system "ps aux | grep #rake_task_name"
then, parse the pid. then, issue a kill command
system "kill #pid"
I haven't tried this yet though.

From within the script, you could run the commands suggested by pat (parsing for the pid) in backticks, system or the %x{} wrapper.

ps afx | grep rake
Then kill the process id
kill -9 "process_id"

Related

how to stop rails server (redmine) ?

i've installed and running redmine in my domain. something went wrong and i cant access redmine admin panel. i tried to reset password and also did some google and changed password in database. but no luck still cant login. then i removed all app files but its still running same as before..
this was the code i used to run redmine server
bundle exec ruby script/rails server webrick -e -s production
now i'm trying to stop or restart but nothing is working.
is there any way to stop the server.
Thanks in advance.
Kill the process
kill -INT $(cat tmp/pids/server.pid)
Its cleaner to write a rake task to do that:
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
Delete the file tmp/pids/server.pid) and then restart the server.
Usually you'll hit Ctrl-C to stop webrick when it's started without -d option. The Ctrl-C makes INT signal, so youcould try with kill -INT <pid> to stop webrick started with -d option.
If it doesn't stop you can try with kill -9 <pid> sending a KILL signal, that's not a proper clean shutdown but seems the only way to stop it. It's not a 'best practice' but it's the only method i've ever found.
$ killall -9 ruby
this command wil kill all the running instance of ruby on your system and you can restart ur server again

Rails Delayed Job production not picking up change in class/job code

I am running a stack of nginx, passenger, rails, delayed_job gem which is running an import.rake task from lib/tasks/ on Ubuntu LTS.
If I make a code change on production to import.rake.
I do a
RAILS_ENV=production script/delayed_job stop
touch tmp/restart.txt
ps aux | egrep '(PID|nginx)'
sudo kill -HUP [PID]
RAILS_ENV=production script/delayed_job start
However, it still does not recognize my change of import.rake. I'm at a loss of what to do. Maybe there is something i'm not thinking of?
I've ran
ps -ef | grep delayed_job
to see if there are any lingering jobs and after running the delayed_job stop command from above all i see is
[server_name] 9426 6168 0 18:46 pts/0 00:00:00 grep --color=auto delayed_job
which shouldn't be an issue. I've also tried just rebooting the server which didn't help.
Any ideas?
Delayed job serializes the instance of your code when it enqueues it, so redeployment won't help unless you change the code that is invoked by your rake task, rather than the rake task itself.
To solve this, decouple the code you change between redeployments from the code that is invoked via delayed job. So, instead of MyLogic.delay.do_stuff, you could do this:
class DelayedTask
def self.do_stuff
self.new.delay.execute
end
private
def execute
MyLogic.do_stuff
end
end
Then just call DelayedTask.do_stuff from your code, and you can change MyLogic.do_stuff in any way you want (without changing the method name or params), ant it will work.

Can't stop rails server

I am new to rails and I am using an ubuntu machine and the rubymine IDE. The problem is that I am unable to stop the rails server. I tried to stop the server by killing the rails process. But, when I run pgrep -l rails, no such process is found. So, I am only able to kill ruby processes, but, the server won't stop.
I tried ./script/server stop (since I started it by running ./script/server start), but, that didn't work. Googling around and finding some stackoverflow posts, I tried to change the localhost port's listening port but without success. Could someone help?
You can use other ports like the following:
rails server -p 3001
Normally in your terminal you can try Ctrl + C to shutdown the server.
The other way to kill the Ruby on Rails default server (which is WEBrick) is:
kill -INT $(cat tmp/pids/server.pid)
In your terminal to find out the PID of the process:
$ lsof -wni tcp:3000
Then, use the number in the PID column to kill the process:
For example:
$ kill -9 PID
And some of the other answers i found is:
To stop the rails server while it's running, press:
CTRL-C
CTRL-Z
You will get control back to bash. Then type (without the $):
$ fg
And this will go back into the process, and then quit out of Rails s properly.
It's a little annoying, but this sure beats killing the process manually. It's not too bad and it's the best I could figure out.
Updated answer:
You can use killall -9 rails to kill all running apps with "rails" in the name.
killall -9 rails
you can use grep command in following way,
ps aux | grep rails
and then
kill -9 {process_id}
pkill -9 rails to kill all the process of rails
Updated answer
ps aux|grep 'rails'|grep -v 'grep'|awk '{ print $2 }'|xargs kill -9
This will kill any running rails process. Replace 'rails' with something else to kill any other processes.
On my MAC the killall -9 rails does not work. But killall -9 ruby does.
Following are steps to kill server process:
1. lsof -i tcp:3000
2. kill -9 1234
where 1234 is the PID of process: localhost:3000 display in step 1.
OR
Remove file(server.pid) under Rails.root/tmp/pids/ and restart server.
OR
open app in another port by using command:
rails s -p 3001
I generally use:
killall ruby
OR
pkill -9 ruby
which will kill all ruby related processes that are running like rails server, rails console, etc.
1. Simply Delete the pid file from rails app directory
Rails_app -> tmp -> pids -> pid file
Delete the file and run
rails start
2. For Rails 5.0 and above, you can use this command
rails restart
If you are using a more modern version of Rails and it uses Puma as the web server, you can run the following command to find the stuck Puma process:
ps aux | grep puma
It will result in output similar to this:
85923 100.0 0.8 2682420 131324 s004 R+ 2:54pm 3:27.92 puma 3.12.0 (tcp://0.0.0.0:3010) [my-app]
92463 0.0 0.0 2458404 1976 s008 S+ 3:09pm 0:00.00 grep puma
You want the process that is not referring to grep. In this case, the process ID is 85923.
I can then run the following command to kill that process:
kill -9 85923
Use ctrl+c to shutdown your Webrick Server.
Unfortunately if its not works then forcefully close the terminal and restart it.
Another trick is that
1. open your system-monitor(a gui application) on ubuntu
2. Select processes tab
3. Then look for a process having name 'ruby'
4. End that process
Delete the server.pid from tmp/pids folder.
In my case, the error was: A server is already running. Check /home/sbhatta/myapp/tmp/pids/server.pid.
So, I delete server.pid
rm /home/sbhatta/myapp/tmp/pids/server.pid
then run rails s
Ctrl-Z should normally do the trick.
Step 1: find what are the items are consuming 3000 port.
lsof -i:3000
step 2 : Find the process named
For Mac
ruby TCP localhost:hbci (LISTEN)
For Ubuntu
ruby TCP *:3000 (LISTEN)
Step 3: Find the PID of the process and kill it.
kill -9 PID
it's as simple as
pkill -9 ruby
nothing more nothing less
I used killall -9 rails like Sri suggested and it didn't work. I adjusted the command to killall -9 ruby and the server closed immediately.
Tl;dr: killall -9 ruby
When the rails server does not start it means that it is already running then you can start by using new port eg.
rails s -p 3001
or it starts and stops in that case you want to delete temp folder in rails directory structure it starts the rails server.
check the /tmp/tmp/server.pid
there is a pid inside.
Usually, I ill do "kill -9 THE_PID" in the cmd
I have noticed on Windows (Im using 10 but not sure if the same for oler). If you use cmd.exe and ctrl + c the raisl server stops correctly.
However, if you use Git Bash, it doesn't. It says it has but when you look at the tmp pids, its still there.
Maybe a bug with git bash?
killall -9 ruby will kill all the ruby processes, and at-least on my machine, rails servers appear as ruby processes. killall -9 rails is much more specific and doesn't work for older versions of rails servers (it gives a 'rails:no process found' because the process is named ruby)
Encountered this problem a while ago. After submitting a form in activeadmin, the rails server just hanged and I was unable to kill it using normal means (even after ctrl+z it was still running in the background). Learner's answer helped, but this command doesn't need process id.
Follow these steps:
open your project
select in tmp folder
select pids folder
delete server.pid file
now start your rails server
On rails 6 using
ps aux | grep rails was not returning the server process
I had to do
ps aux | grep puma
to find the actual process and then kill it using
kill -9 {process_id}
It is late for this question.
Here is my 2 cents. I made a rake task for stopping the server when I don't have access to it.
I only tested on Mac though.
With this you can simply add it to your project then run the rake command.
Here you go:
Gist link: -latest version will be here.
https://gist.github.com/houmanka/289184ca5d8d92de0499#file-server-rake
Some code in here:
# Make a file under: `project_root/lib/tasks/server.rake`
# Then paste the following code
namespace :server do
desc "Stop the running server by killing the PID"
task :kill do
STDOUT.puts "Enter port number: "
post_number = STDIN.gets.strip
system "pid=$(lsof -i:#{post_number.to_i} -t); kill -TERM $pid || kill -KILL $pid"
end
end
# Then to use it in the terminal: `rake server:kill`
Also, Make sure that you are doing command Cntrl+C in the same terminal (tab) which is used to start the server.
In my case, I had 2 tabs but i forgot to stop the server from correct tab and i was wondering why Cntrl+C is not working.
One super easy way would be
gem install shutup
then go in the current folder of your rails project and run
shutup # this will kill the Rails process currently running
You can use the command 'shutup' every time you want
DICLAIMER: I am the creator of this gem
NOTE: if you are using rvm install the gem globally
rvm #global do gem install shutup
For my windows 10 machine, Ctrl - C + Ctrl - D works.
We can kill rails session on Linux using PORT no
fuser -k 3000/tcp
here 3000 is a port no.
Now restart your server, you will see your server is in running state.
Just open the file using the location given
sudo vi /Users/user1/go/src/github.com/rails_app/rails_project/tmp/pids/server.pid
find the process_id / thread_id at which the process is runnning.
Kill the specified process / thread using kill -9 84699
Press Ctrl - C it will stop
if not check

How to send a kill signal to stop a Rails app?

If you start a Rails app with the script/server command, what command do you use to stop it?
You may not have a pid file to kill if you are using a server like webrick, but you can get at it with:
ps aux | grep ruby
which should show you all your Ruby processes, then kill the process running the server: kill x, where x is the process number.
Note that you will need to be running as the user running the Ruby command, otherwise sudo is in your future.
cat out the following file in your Rails tree:
tmp/pids/server.pid
and send any variety of kill command to that pid, or simply execute the following (assuming your tree looks like: /my/rails/app:
kill -9 $(cat /my/rails/app/tmp/pids/server.pid)
pgrep ruby
to see what servers are running and then:
kill -9 serverNumber
To kill the server.

How to stop a Daemon Server in Rails?

I am running my rails application using the following
$script/server -d webrick
on my Ubuntu system , above command run the webrick server in background . I could kill the process using kill command
$kill pid
Does rails provide any command to stop the background running daemon server ?
like the one provided by rails to start the server , Thanks .
EDIT When it is appropriate to start the daemon server ? Any real time scenario will help Thanks
if it can be useful, on linux you can find which process is using a port (in this case 3000) you can use:
lsof -i :3000
it'll return the pid too
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.
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
The only proper way to kill the Ruby on Rails default server (which is WEBrick) is:
kill -INT $(cat tmp/pids/server.pid)
If you are running Mongrel, this is sufficient:
kill $(cat tmp/pids/server.pid)
Use kill -9 if your daemon hung. Remember the implications of kill -9 - if the data kept in Active Record caches weren't flushed to disk, you will lose your data. (As I recently did)
The process id of the daemon server is stored in your application directory tmp/pids/. You can use your standard kill process_id with the information you find there.
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>
pguardiario beat me to it, though his implementation is a bit dangerous since it uses SIGKILL instead of the (recommended) SIGINT. Here's a rake task I tend 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 issues an interrupt to the server if and only if the pidfile exists. It doesn't throw unsightly errors if the server isn't running, and it notifies you if it's actually shutting the server down.
If you notice that the server doesn't want to shut down using this task, add the following line after the Process.kill "INT" line, and try to upgrade to a kernel that has this bug fixed.
Process.kill "CONT", pid
(Hat tip: jackr)
A Ruby ticket, http://bugs.ruby-lang.org/issues/4777, suggests it's a kernel (Linux) bug. They give a work around (essentially equivalent to the Ctrl-C/Ctrl-Z one), for use if you've demonized the server:
kill -INT cat tmp/pids/server.pid
kill -CONT cat tmp/pids/server.pid
This seems to cause the original INT signal to be processed, possibly allowing data flush and so on.
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]
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.
i don't think it does if you use -d. I'd just kill the process.
In the future, just open up another terminal window instead and use the command without -d, it provides some really useful debugging output.
If this is production, use something like passenger or thin, so that they're easy to stop the processes or restart the servers
one-liner: kill -INT `ps -e | grep ruby | awk '{print $1}'`
ps -e lists every process on the system
grep ruby searches that output for the ruby process
awk passes the first argument of that output
(the pid) to kill -INT.
Try it with echo instead of kill if you just want to see the PID.
if kill process not works, then
delete file server.pid from MyRailsApp/tmp/pids/
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...
You can start your server in the background by adding -d to your command. For instance:
puma -d
To stop it, just kill whatever process is running on port 3000:
kill $(cat tmp/pids/server.pid)

Resources