start unicorn on startup - ruby-on-rails

I'm using Rails 4 + Nginx + Unicorn and I want that when my raspberry pi is rebooted it should run Rails application on startup.
Issue: The Nginx is running but unicorn is not running.
I've put in /etc/rc.local file the following command
cd /opt/www/RAILSAPP/current && bundle exec unicorn -E production -c config/unicorn.rb -D
but its not up when I'm rebooting. when I'm accessing the pi via ssh and run the command its working.
How can I start unicorn on startup in order to serve the rails app?

Related

Ubuntu run Puma and Sidekiq in background as Daemonized processes

Two years ago I was running the following command:
bundle exec puma -C config/puma.rb -b unix:/home/user/site/shared/tmp/sockets/user-puma.sock -d
bundle exec sidekiq -d
But now, after some Ubuntu updates, the -d flag is deprecated and I can't start the Puma and Sidekiq processes to run in background.
I also tried running:
bundle exec puma -C config/puma.rb -b unix:/home/user/site/shared/tmp/sockets/user-puma.sock &
bundle exec sidekiq &
This works only while I'm logged on the SSH, when I close the SSH connection, all processes opened with & are closed.
How can I run Puma, Sidekiq and other processes in background as Daemons?
What works for me is setting up my own systemd service for sidekiq
A well-documented example file is in the sidekiq github repository
Along with this, I'd also recommend using monit to watch background processes... here's a recipe from Lugo Labs that I loosely base my deploys off of

How to start redmine with a specific port from bash script?

On my computer, I can start redmine by:
cd /usr/share/redmine
bundle exec rails server webrick -e production -p 3001
and this will start redmine just fine at port 3001.
If I create a bash script, it always starts at port 3000, like the parameters were ignored.
What am I doing wrong ?
This is what I have
#!/bin/bash
#exec /usr/lib/x86_64-linux-gnu/opera/opera --no-sandbox $#
cd /usr/share/redmine
# Port from settings file will be ignored either way ...
#bundle exec rails -p 3003 server webrick -e production
bundle exec rails server webrick -e production -p 3001
Quickly checked that and found that your script works perfectly fine for me, changing the port in the script also changes the port WEBrick is listening at.
So nothing wrong with the script.

How to start passenger production when ubuntu or server starts?

I developed a Ruby On Rails application and want to deploy in production (intranet).
As of now i created an sh file to run the passenger like so:
cd /pathofmyapp
passenger start -a 0.0.0.0 -p 3000 -d -e production
cd /pathofmyapp2
passenger start -a 0.0.0.0 -p 3000 -d -e production
When the ubuntu server starts i have to execute the sh or manually start the passenger.
Is their a way to automate this so that whenever the ubuntu server starts, the command will automatically run or the ruby on rails application will start?
Thanks in advance.
You can add that to your cronjob to run the code on reboot.
#reboot cd /pathofmyapp && passenger start -a 0.0.0.0 -d -e production

Foreman start and hide the web process output

I'm using Foreman to run my Rails 4 application, and I'm getting duplicate output. Web shows pretty much the same info as Log.
Can I run foreman and just hide the Web output?
My procfile:
web: bundle exec passenger start -p $PORT --max-pool-size 3
log: tail -f ./log/development.log
The entries in your Procfile are just commands to run and you can tinker with them the same way you would on the command line.
So you could redirect all of the stdout for your web process to /dev/null (looks like you might be on a *nix system):
web: bundle exec passenger start -p $PORT --max-pool-size 3 > /dev/null

start unicorn app server when the ubuntu server starts

I am running my rails application using ruby enterprise edition with unicorn as app server. I run this command
bundle exec unicorn -D -c /home/ubuntu/apps/st/config/unicorn.rb
I need to run this command soon after the system reboots or starts. I am running the app on ubuntu 10.04 LTS EC2 instance. I tried couple of examples which are mentioned on this site as well as this site but it’s not working for me. Any heads up
Try it as an Upstart. To do so, you need to create a myapp.conf file into the directory /etc/init/ with the contents below:
description "myapp server"
start on runlevel [23]
stop on shutdown
exec sudo -u myuser sh -c "cd /path/to/my/app && bundle exec unicorn -D -c /home/ubuntu/apps/st/config/unicorn.rb"
respawn
After that, you should be able to start/stop/restart your app with the commands below:
start myapp
stop myapp
restart myapp
Use ps -aux | grep myapp to check if your app is running.
You can use this file as a template, set appropriate paths mentioned in this file, make it executable and symlink into /etc/init.d/my_unicorn_server. Now you can start the server using:
sudo service my_unicorn_server start
Then you can do:
sudo update-rc.d my_unicorn_server defaults
To startup the unicorn server on system reboot automatically.
In my case, I just wanted it quick so I place the startup command in /etc/rc.local like below. Note that i'm using RVM.
# By default this script does nothing.
cd <your project dir>
/usr/local/rvm/gems/ruby-2.2.1/wrappers/bundle exec unicorn -c <your project dir>/config/unicorn.conf -D
test -e /etc/ssh/ssh_host_dsa_key || dpkg-reconfigure openssh-server
exit 0
Make sure your startup command is above the exit 0. After you reboot, check whether it is running or not by directly hitting the url of your application or use ps -aux | grep unicorn command.
Note* Previously I use Phusion Passenger but I'm having trouble to see its error log, so I move back to unicorn. I also tried #warantesbr without success, which I guess it fails because my whole environment where setup using root access.
If you are using unicorn_init script
You can configure a cron job to start the unicorn server on reboot
crontab -e
and add
#reboot /bin/bash -l -c 'service unicorn_<your service name> start >> /<path to log file>/cron.log 2>&1'

Resources