How to keep server running on EC2 after ssh is terminated - ruby-on-rails

I have an EC2 instance on which I installed rails server. The server also runs fine when I do
rails server
But after I close the ssh connection the server also stops. How can I keep the server running even after closing the ssh connection.

screen rails s
did the trick
after that CTRL + A + D and I left and the server is running fine

Try this. We have to start rails server as daemon.
rails s -d &

run at as server means thu nginx or apache or what ever this development server not mean run as server
user this is need more info https://www.digitalocean.com/community/articles/how-to-install-rails-and-nginx-with-passenger-on-ubuntu
also if want advance sololtion use rubber https://github.com/rubber/rubber

I needed mine running everything not, just rails in the background. Install Screen which makes a sub terminal that isn't affected by your ssh connection. sudo apt-get install screen Open screen screen Then start rails rails server &.
Press 'Crtl + A' then 'D' to escape and type screen -r to get back in to the screen terminal.

I will recommend using apache or something else instead of the regular rails server but you can probably add & at the end and feel free to leave
rails server &

These steps worked for me. MY OS is Description: Ubuntu 16.04.4 LTS
sudo apt-get install screen
screen rails s
CTRL + A + D from terminal to detached the existing process and let it run.

Here's a production proof version using RVM and Systemd. Will keep the server alive if it gets terminated for any reason.
[Unit]
Description=Puma Control
After=network.target
[Service]
Type=forking
User=user
WorkingDirectory=/var/www/your_project_name
PIDFile=/var/www/your_project_name/shared/tmp/pids/puma.pid
ExecStart=/home/user/.rvm/bin/rvm default do bundle exec puma -C /var/www/your_project_name/shared/puma.rb --daemon
ExecStop=/home/user/.rvm/bin/rvm default do bundle exec pumactl -S /var/www/your_project_name/shared/tmp/pids/puma.state -F /var/www/your_project_name/shared/puma.rb stop
Restart=always
# RestartSec=10
[Install]
WantedBy=default.target

Related

EC2 : Rails deployment gives blank pages

I have deployed my rails application on EC2. It runs on two servers. One for rails application and second for DB.
When I start application using "rails s -e production&" and if I stay connected using SSH,
I can see the webpages.
As soon as I disconnect SSH I can not see the pages.
There are no errors thrown. One weird thing is "Production.log" file does not have anything.
everything is spit out on console.
You are running rails in the current ssh session. Any programs you have running during that session will stop if you disconnect. You need to set up your rails app to run as a daemon using something like Phusion Passenger.
You are basically running the built in WEBrick server that is not really meant for production so it's likely that the process is getting killed after the parent process (your ssh process) gets terminated.
You can probably tweak the configuration to make WEBrick not quit, or you can simply run your session using screen or tmux
Screen:
$ screen
$ rails s -e production &
$ screen -d
When you want to reattach:
$ screen -r
Tmux:
$ tmux
$ rails s -e production &
$ # Hit <ctrl-b><ctrl-d> to detach
When you want to reattach:
$ screen attach -t 0
Or like #datasage mentioned you can run your Rails with an actual production web server like Passenger Phusion or Unicorn.

Run thin as webservice Ruby 1.9.3, Windows Server 2003

I'm trying to run a ruby app (redmine) as thin webservice on Windows Server 2003. I've already read and done everything said here and here, but it still won't work. At the moment I've set:
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\[my_service_name]\Parameters]
Application=c:\ruby\bin\ruby.exe
AppDirectory="c:\Program Files\redmine-1.4-test"
AppParameters=c:\ruby\bin\thin start -p 3001 -e development
Running thin from command line via:
C:\Program Files\redmine-1.4-test>thin start -p 3001 -e development
works fine.
I already tested moving Redmine to a path with no spaces, which doesn't change anything.
Any idea what can be wrong? Any hints how I could track the problem down?
Make sure that you are running the new service in the account where Ruby is installed - where you can run thin normally from the command line. Edit the service from the Control Panel (services.msc) and set up the Logon tab accordingly.

How to stop (and restart) the Rails Server?

I'm following the instructions here http://railsinstaller.org/mac to get up and running with Rails on a Mac running OS X 10.8.2
At step 8 I'm asked to restart Rails server but how?
I'm assuming via a command line, but from within the already open ruby terminal window or a new one?
Now in rails 5 you can do:
rails restart
The output of rails --tasks:
Restart app by touching tmp/restart.txt
I think that is usefully if you run rails as a demon
Press Ctrl+C
When you start the server it mentions this in the startup text.
On OSX, you can take advantage of the UNIX-like command line - here's what I keep handy in my .bashrc to enable me to more easily restart a server that's running in background (-d) mode (note that you have to be in the Rails root directory when running this):
alias restart_rails='kill -9 `cat tmp/pids/server.pid`; rails server -d'
My initial response to the comment by #zane about how the PID file isn't removed was that it might be behavior dependent on the Rails version or OS type. However, it's also possible that the shell runs the second command (rails server -d) sooner than the kill can actually cause the previous running instance to stop.
So alternatively, kill -9 cat tmp/pids/server.pid && rails server -d might be more robust; or you can specifically run the kill, wait for the tmp/pids folder to empty out, then restart your new server.
In case that doesn't work there is another way that works especially well in Windows: Kill localhost:3000 process from Windows command line
if you are not able to find the rails process to kill it might actually be not running. Delete the tmp folder and its sub-folders from where you are running the rails server and try again.
I had to restart the rails application on the production so I looked for an another answer. I have found it below:
http://wiki.ocssolutions.com/Restarting_a_Rails_Application_Using_Passenger
I just reboot the server with
Ctrl + c
That worked for me

How to start Thin server automatically when System Re boots

How to start thin Server automatically when Server Reboots.
I have a Rails 3 project which uses Thin Server.I can manually control the thin Server from terminal.Is it possible to start thin server as background process when system reboots.
Thanks in advance.
You can use Scheduled Tasks. There's a specific trigger option for starting a task when the computer starts.
To start the process in background mode you can use the -d option of the rails command.
I suppose you need to do this:
sudo thin install #to create init.d entry for thin
sudo /usr/sbin/update-rc.d -f thin defaults # to setup it
sudo thin config -C /etc/thin/<appname>.yml -c /var/rails/<appdir> --servers 4 -e production # to generate congig file for it. If you already got config file, you can just copy it to /etc/thin/ instead of creating.
If you use rvm on your server - browse this: RVM and thin, root vs. local user.
You can also take a look at: https://github.com/opscode-cookbooks/runit

How to run thin in the vps after the terminal window is closed

Hey guys
After several weeks of local testing, I'm now setting up a VPS, and try to run rails on it. At this point, I can open up a Terminal session and ssh to the VPS, run the thin start, then I the server is running ok, But as soon as I closed the terminal the thin is down.
How can I make thin server running in the VPS all the time?
another question how to change from test mode to production mode in rails.
Thanks
Demonize thin, run it with -d flag.
thin -d
For the task at hand you will want to use a tool called Screens
Install it:
sudo apt-get install screen
Then to run it you run:
screen -d executable
To put screen to background: Ctrl+D
To recall a screen: screen -r.
You should be all good now.
You don't really want to launch and stop thin by hand. You want it to be a daemon to be started when your system start and to be managed like any other daemons (e.g. nginx, syslog, sshd, etc.). How to do this is very distribution-dependent, but you should definitely have a look at /etc/init.d/ or /etc/rc.d/ and /etc/rc.conf.
To go in production mode within the command line you use thin -e production, but the preferred way should be to specify it in thin's configuration files. You should have (or create) an /etc/thin/ folder, with one .yml file for each application you're deploying.

Resources