Foreman Cannot Start Nginx, But I Can Start it Manually. Why? - ruby-on-rails

I am currently running Foreman on staging (Ubuntu) and once I get it working will switch to using upstart.
My Procfile.staging looks like this:
nginx: sudo service nginx start
unicorn: bundle exec unicorn -c ./config/unicorn.rb
redis: bundle exec redis-server
sidekiq: bundle exec sidekiq -v -C ./config/sidekiq.yml
I can successfully start nginx using:
$ sudo service nginx start
However when I run $ foreman start, whilst the other three processes start successfully, nginx does not:
11:15:46 nginx.1 | started with pid 15966
11:15:46 unicorn.1 | started with pid 15968
11:15:46 redis.1 | started with pid 15971
11:15:46 sidekiq.1 | started with pid 15974
11:15:46 nginx.1 | Starting nginx: nginx.
11:15:46 nginx.1 | exited with code 0
11:15:46 system | sending SIGTERM to all processes
SIGTERM received
11:15:46 unicorn.1 | terminated by SIGTERM
11:15:46 redis.1 | terminated by SIGTERM
11:15:46 sidekiq.1 | terminated by SIGTERM
So why isn't nginx starting when started by Foreman?

The is a problem in your Procfile.
The nginx command can't use sudo inside foreman, because it will always ask for a password and then it will fail. That's why you are not starting nginx and the logs are empty.
If you really need to use sudo inside a procfile you could use something like this:
sudo_app: echo "sudo_password" | sudo -S app_command
nginx: echo "sudo_password" | sudo -S service nginx start
which I really don't recommend. Other option is to call sudo foreman start
For more information check out this issue on github, it is precisely what you want to solve.
Keep me posted if it works for you.

You should be able to add sudo access without a password for your local user to allow managing this service. This can be a big security hole, but if you whitelist what commands can be run you dramatically reduce the risk. I recommend adding no-password sudoers entry for the services command and anything else you want to script:
/etc/sudoers:
your_user_name ALL = (ALL) NOPASSWD: /usr/sbin/service
Another option if you're not comfortable with this would be to run nginx directly, not through the service manager:
nginx: /usr/sbin/nginx -c /path/to/nginx.conf

Related

How to stop a nohup process running rails server on port 3000

I have started a rails server puma by using the following command.
nohup rails server &
its output was [2] 22481 along with the following:
nohup: ignoring input and appending output to 'nohup.out'
But now I have forget the returned process id, so how can I detect the process id so as to delete the process on aws.
To kill whatever is on port 3000 (webrick server default port), type this below command to get process id for 3000 port:
$ lsof -wni tcp:3000
Then, use process id (PID) to kill the process:
$ kill -9 PID
Rails server process pid can be found in this directory:
-> tmp/pids/server.pid
then,
Kill -9 pid
command
ps -ef
return the full output list of processes in which one of the list item is as:
ec2-user 12992 1 0 Dec20 ? 00:00:57 puma 3.12.0 (tcp://0.0.0.0:3000) [tukatech_garmentstore_live]
so force killed the process by.
kill -9 12992
did the job
ps aux|grep 3000
This will give you rails server id running on port 3000

can't open Puma server on port 3000 and can't find any processes to kill

I am running Puma on Rails 4.2.1 application.
When I run the following command to start the server:
rails s
I get the following error:
puma-3.4.0/lib/puma/binder.rb:255:in `initialize': Address already in use - bind(2) for "localhost" port 3000 (Errno::EADDRINUSE)
When I look for the process that is running to kill it:
ps aux | grep ruby
I get the following:
mitchellgould 52496 0.0 0.0 2432772 648 s000 S+ 3:34PM 0:00.01 grep ruby
When I run:
sudo kill -9 52496
I get:
kill: 52496: No such process
I can start the server in another port but I would like to remove this issue and understand what is happening. Any help appreciated.
ps aux | grep puma
or
ps aux | grep rails
then copy its pid and kill the process:
kill -9 PUMAS_PID
and run the server again
chell.
Try to check port 3000, maybe this is not ruby.
lsof -i tcp:3000
Fast solution:
You can restart your OS, all ports will be reset...
Delete pid with server (your_project/tmp/pids/server.pid)
Puma
Check your config for Puma in your project and how you start puma server.
Puma start with Rails

Rails: Address already in use - bind(2) (Errno::EADDRINUSE)

I am trying to deploy Rails app with the Puma web server. When trying to start Puma server with a config file bundle exec puma -C config/puma.rb I get an error that the address is already in use.
Does someone know how to fix this?
bundle exec puma -C config/puma.rb
[23699] Puma starting in cluster mode...
[23699] * Version 2.11.3 (ruby 2.0.0-p353), codename: Intrepid Squirrel
[23699] * Min threads: 5, max threads: 5
[23699] * Environment: development
[23699] * Process workers: 2
[23699] * Preloading application
Jdbc-MySQL is only for use with JRuby
[23699] * Listening on tcp://0.0.0.0:3000
/.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:210:in `initialize': Address already in use - bind(2) (Errno::EADDRINUSE)
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:210:in `new'
from /Users/lexi87/.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:210:in `add_tcp_listener'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:96:in `block in parse'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:82:in `each'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:82:in `parse'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/runner.rb:119:in `load_and_bind'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/cluster.rb:302:in `run'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/cli.rb:216:in `run'
from /rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/bin/puma:10:in `<top (required)>'
from /.rvm/gems/ruby-2.0.0-p353/bin/puma:23:in `load'
from /.rvm/gems/ruby-2.0.0-p353/bin/puma:23:in `<main>'
from /.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval'
from /.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>'
You need to use kill -9 59780 with 59780 replaced with found PID number (use lsof -wni tcp:3000 to see which process used 3000 port and get the process PID).
Or you can just modify your puma config change the tcp port tcp://127.0.0.1:3000 from 3000 to 9292 or other port that not been used.
Or you can start your rails app by using:
bundle exec puma -C config/puma.rb -b tcp://127.0.0.1:3001
To kill the puma process first run
lsof -wni tcp:3000
to show what is using port 3000. Then use the PID that comes with the result to run the kill process.
For example after running lsof -wni tcp:3000 you might get something like
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 3366 dummy 8u IPv4 16901 0t0 TCP 127.0.0.1:3000 (LISTEN)
Now run the following to kill the process. (where 3366 is the PID)
kill -9 3366
Should resolve the issue
you can also try this trick:
ps aux | grep puma
sample output:
myname 77921 0.0 0.0 2433828 1972 s000 R+ 11:17AM 0:00.00 grep puma
myname 67661 0.0 2.3 2680504 191204 s002 S+ 11:00AM 0:18.38 puma 3.11.2 (tcp://localhost:3000) [my_proj]
then:
kill -9 67661
Found the script below in this github issue. Works great for me.
#!/usr/bin/env ruby
port = ARGV.first || 3000
system("sudo echo kill-server-on #{port}")
pid = `sudo lsof -iTCP -sTCP:LISTEN -n -P | grep #{port} | awk '{ print $2 }' | head -n 1`.strip
puts "PID: #{pid}"
`kill -9 #{pid}` unless pid.empty?
You can either run it in irb or inside a ruby file.
For the latter, create server_killer.rb then run it with ruby server_killer.rb
SOLUTION FOR GENERAL Address already in use - bind(2) (Errno::EADDRINUSE)
This issue is because we are trying to use the same port which is already is use. so we have to stop the services running on that port so that we can run another services.
we can use kill like kill -9 {PID}where {PID} is the PID of the services running on that port. To know the PID of any services lets say "firefox" we can use commands like pidof firefox, ps aux | grep -i firefox ,pgrep firefox and then use the kill command to stop that service.
sometime we might get into the situation where we don't know the PID or the service name to search for in this case we can use the following little ruby code to do it for us.(in this case port 3000, you can change it according to your need)
#!/usr/bin/env ruby
port = ARGV.first || 3000
system("sudo echo kill-server-on #{port}")
pid = `sudo lsof -iTCP -sTCP:LISTEN -n -P | grep #{port} | awk '{ print $2 }' | head -n 1`.strip
puts "PID: #{pid}"
`kill -9 #{pid}` unless pid.empty?
save it as something.rb and run sudo ruby something.rb
If the above solutions don't work on ubuntu/linux then you can try this
sudo fuser -k -n tcp port
Run it several times to kill processes on your port of choosing. port could be 3000 for example. You would have killed all the processes if you see no output after running the command
You can find and kill the running processes: ps aux | grep puma
Then you can kill it with kill PID
I had this issue on my Macbook Air, running Rails 5.0.3, Puma 5.2.2
Tried running
lsof -wni tcp:3000 but there's no process on this port number.
Managed to fix this by running:
export PORT=3000 on my terminal, then I just added this extra line to my .bash_profile
It might be old but in my case, it was because of docker. Hope it will help others.

Foreman terminates immediately

I recently installed OSX and Ubuntu on different computers. I then attempted to install redis and foreman for both OS's. Both errors threw no flags, and seemed to execute successfully. However, whenever I go to start foreman with foreman start, I run into the below issue on both computers:
23:48:35 web.1 | started with pid 1316
23:48:35 redis.1 | started with pid 1317
23:48:35 worker.1 | started with pid 1318
23:48:35 redis.1 | [1317] 11 Jun 23:48:35.180 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
23:48:35 redis.1 | [1317] 11 Jun 23:48:35.181 * Increased maximum number of open files to 10032 (it was originally set to 256).
23:48:35 redis.1 | [1317] 11 Jun 23:48:35.181 # Creating Server TCP listening socket *:6379: bind: Address already in use
23:48:35 redis.1 | exited with code 1
23:48:35 system | sending SIGTERM to all processes
23:48:35 worker.1 | terminated by SIGTERM
23:48:35 web.1 | terminated by SIGTERM
For some reason, it seems like a path issue to me because it seems like Redis or Foreman cannot find the files they need to use to successfully execute, but I'm not exactly sure.
On OSX I used gem install foreman and Brew install Redis .
On Ubuntu I used the following:
Redis:
$ cd ~
$ wget http://download.redis.io/redis-stable.tar.gz
$ tar xvzf redis-stable.tar.gz
$ cd redis-stable
$ make
$ make test
Foreman:
$ gem install foreman
My PATH on OSX is as follows:
/Users/c/.rvm/gems/ruby-2.1.0/bin:/Users/c/.rvm/gems/ruby-2.1.0#global/bin:/Users/c/.rvm/rubies/ruby-2.1.0/bin:/Users/c/.rvm/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
On Ubuntu, my PATH is:
/usr/local/bin:/usr/lib/postgresql:/usr/lib/postgresql/9.3:/usr/lib/ postgresql/9.3/lib:/usr/lib/postgresql/9.3/bin:/usr/share/doc:/usr/share/doc/postgresql-9.3:/usr/share/postgresql:/usr/share/postgresql/9.3:/usr/share/postgresql/9.3/man:$PATH
Redis-server does seem to execute successfully once, and then it fails with the message:
[1457] 12 Jun 00:02:48.481 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
[1457] 12 Jun 00:02:48.482 * Increased maximum number of open files to 10032 (it was originally set to 256).
[1457] 12 Jun 00:02:48.483 # Creating Server TCP listening socket *:6379: bind: Address already in use
Trying $ redis-server stop returns:
[1504] 12 Jun 00:05:56.173 # Fatal error, can't open config file 'stop'
I need help figuring out how to get Foreman and Redis working together so that I can view my local files in the browser at 127.0.0.1
EDIT
Redis does start, but nothing happens when I navigate to localhost:6379. I also tried the suggestion of finding processes. It found
c 751 0.0 0.0 2432768 596 s005 R+ 2:03PM 0:00.00 grep redis
c 616 0.0 0.0 2469952 1652 s004 S+ 2:01PM 0:00.05 redis-server *:6379
Trying to kill the process results in
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec
... or kill -l [sigspec]
Try starting Redis server with the following command :
redis-server <path to your config file>
Also, check if there's an instance of Redis server already running by
ps aux | grep redis
and then if process is found :
kill <process id>
Restart your redis server.
This one liner will kill any existing redis-servers and then start a new redis-server. When run in Foreman it doesn't send a SIGTERM which causes Foreman to quit, sending a SIGINT lets Foreman continue.
(ps aux | grep 6379 | grep redis | awk '{ print $2 }' | xargs kill -s SIGINT) && redis-server
In Procfile.dev:
redis: (ps aux | grep 6379 | grep redis | awk '{ print $2 }' | xargs kill -s SIGINT) && redis-server
List the redis server running using terminal
command : ps aux | grep redis
In list note down 'pid' number of the server which you want to terminate Example pid: 5379
use command : kill 5379
Try this:
$ sudo systemctl stop redis-server
now run: foreman start

issue getting rack_env with foreman / heroku facebook app

Having a hard time getting my local setup properly. Trying to run foreman and getting an issue:
web: bundle exec thin -R config.ru start -p $PORT -e ${RACK_ENV:-development}
RACK_ENV:"FACEBOOK_APP_ID=275479742529226"
RACK_ENV:"FACEBOOK_SECRET=xxx"
running "foreman start"
15:21:21 RACK_ENV.1 | process terminated
15:21:21 web.1 | started with pid 23406
15:21:21 system | sending SIGTERM to all processes
15:21:21 RACK_ENV.1 | started with pid 23407
15:21:21 system | sending SIGTERM to pid 23406
15:21:21 RACK_ENV.1 | started with pid 23408
15:21:21 RACK_ENV.1 | /Users/mc/.rvm/gems/ruby- 1.9.2-p290/gems/foreman-
0.41.0/bin/foreman-runner: line 36: exec: "FACEBOOK_APP_ID=275479742529226": not found
apparently I'm setting my fb keys completely incorrect for the env.. if anyone could help out. I've tried setting up a .env file in the root as well, which contain
FACEBOOK_APP_ID=964173273189
FACEBOOK_SECRET=xxx
Removing everything from the procfile (which I believe is incorrect anyways) and running foreman, shouldn't it pickup the .env file in the root by default? which isn't proving any luck.
5:44:20 web.1 | started with pid 23705
15:44:24 web.1 | missing env vars: please set FACEBOOK_APP_ID and FACEBOOK_SECRET
with your app credentials
15:44:24 web.1 | process terminated
15:44:24 system | sending SIGTERM to all processes
Thanks
I don't think the issue is that foreman isn't finding .env, it's that the syntax of your Procfile is incorrect. Every line in the Procfile after the : should executable on the command line.
Create your Procfile with the following contents:
web: bundle exec thin -R config.ru start -p $PORT -e $RACK_ENV
And set your .env file to:
RACK_ENV=development
FACEBOOK_APP_ID=1234
FACEBOOK_SECRET=xyz
Note the RACK_ENV var needs to be explicitly set whereas PORT does not since Foreman automatically sets it.

Resources