How to start on booting "rvmsudo rails server" command - ruby-on-rails

I am trying to configure my own "Thingspeak" server on Ubuntu 12.04 LTS virtual machine. I introduced this commands on terminal (installation on clean install of ubuntu): https://github.com/iobridge/ThingSpeak but at the final I use:
rvmsudo rails server -d -b <myip> -p 80
This command let me run Thingspeak app on port 80 as a daemon. It works well, but when the server reboot (sudo reboot for example) the thingspeak server shut down and I don't want this, I want to automatically (on booting or startup) start the thingspeak server so I don't need to manually use in terminal the rvmsudo command again.
How can I achieve this?

You can add to:
/etc/rc.local
rvmsudo rails server -d -b -p 80
or add it to /etc/init.d as well
Updating this:
sudo nano /etc/rc.local
Add the following before the exit 0 line:
cd <folder path>
rvmsudo rails server -d -b -p 80

Related

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.

Shell into a Docker container running on a Heroku dyno. How?

Given a dyno in which a container is running, what's the Heroku equivalent of docker exec -it blarg /bin/bash? That is, how can one open a shell into the already-running container?
Example Dockerfile:
FROM heroku/heroku:16
CMD while true; do sleep 1; done
Example run:
$ heroku container:push my_app
<wait a minute>
$ heroku ps
=== my_app (Free): /bin/sh -c while\ true\;\ do\ sleep\ 1\;\ done (1)
my_app.1: up 2017/10/09 12:13:07 -0600 (~ 4m ago)
So far so good.
But now...
$ heroku ps:exec --dyno=my_app.1
Establishing credentials... error
▸ Could not connect to dyno!
▸ Check if the dyno is running with `heroku ps'
For good measure I check heroku ps at this point and it shows that the dyno is still running.
Yes, I've done all the things Heroku suggests to enable Docker support. Per the documentation, I have tried using a base image of my choice while ensuring that bash, curl, openssh, and python are present. I have also tried using the Heroku-16 base image, as shown in the above example.
(The linked documentation also references steps required for Private Spaces. Since I'm not using Private Spaces I have not applied those steps.)
EDIT CIRCA 2022 This was the accepted answer in 2017. I'm not so sure anymore, so I'm unaccepting my answer here to avoid misleading anyone. I'm not fiddling with Heroku + Docker these days, so I'm not in a good position to accept an answer.
TL;DR Ensure that bash is installed in the image and add this to your Dockerfile:
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
Explanation
Contrary to what the documentation would lead one to believe, Heroku does not, out of the box, support heroku ps:exec into a Docker container already running in a dyno.
Quoting from responses I have received from the Heroku team:
Our ps:exec feature ... works ... by injecting a bash file into dynos,
opening an additional port in the background, and allowing you to
connect to it.
[T]he default
shell used by Docker is /bin/sh, which is not compatible with the
Heroku Exec script (it's requires /bin/bash).
There is a workaround you can use though. Put the following in your
Dockerfile:
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
This is definitely a gap in
our product, and we'll work to make this better.
If bash is installed, run heroku run bash. This will get you into the shell from your command line.
You can also use the GUI and go to "More" -> "Run Console" on your heroku app, and input "bash" to bring it up there.
Edited:
In order to run heroku ps:exec on apps with Docker and deployed via the Container Registry you have to enable runtime-heroku-exec.
You can do heroku features:enable runtime-heroku-exec to enable it
Here you can see the documentation of exec with the instructions to enable docker support
in my situation, to get this working with Ubuntu 20.04 (focal), i had to additionally install the python-is-python3 package into the docker image, to get heroku-exec working.
here is a working example (october 2020) of an ubuntu-based dockerfile, that works with heroku-exec:
FROM ubuntu:focal
# install required packages
RUN export DEBIAN_FRONTEND=noninteractive && apt-get update && apt-get install -y python3 curl python-is-python3 openssh-server iproute2 nginx && apt-get clean
# simplfy nginx config to enable ENV variable substitution
RUN echo 'server { listen PORT_NUMBER; }' > /etc/nginx/sites-enabled/default
# add config required for HEROKU_EXEC
# ENV HEROKU_EXEC_DEBUG=1
RUN rm /bin/sh \
&& ln -s /bin/bash /bin/sh \
&& mkdir -p /app/.profile.d/ \
&& printf '#!/usr/bin/env bash\n\nset +o posix\n\n[ -z "$SSH_CLIENT" ] && source <(curl --fail --retry 7 -sSL "$HEROKU_EXEC_URL")\n' > /app/.profile.d/heroku-exec.sh \
&& chmod +x /app/.profile.d/heroku-exec.sh
# configure NGINX to listen on dynamic $PORT env variable supplied by Heroku.
CMD sed -i 's/PORT_NUMBER/'"$PORT"'/g' /etc/nginx/sites-enabled/default; nginx -g 'daemon off;'
then connect with this command:
heroku ps:exec -a name-of-app-12345
Both heroku run /bin/bash and heroku ps:exec won't work in my situation. The former opens a new container which is different from the real one running! The latter just doesn't work in my container of alpine3, though heroku features:enable runtime-heroku-exec can succeed. My solution is to bring up a shell server and a traffic forwarder in the container. Then on the client connect to the shell server with tunnel created by traffic forwarder.
traffic flow:
localhost:2023 -> chisel client -> ...tunnel... -> chisel server -> localhost:8182
In conainer, start up a shell server with socat and a tunnel server with chisel:
nohup socat tcp-l:8182,reuseaddr,fork exec:/bin/bash,pty,setsid,setpgid,stderr,ctty > /tmp/socat.log 2>&1 &
nohup ./chisel server --port $PORT --proxy http://httpbin.org > /tmp/chisel.log 2>&1 &
On client side, start a chisel client to forward traffic from localhost:8182 to the socat on the server
chisel client http://yourapp.herokuapp.com/ 0.0.0.0:2023:localhost:8182
on client side, open another terminal window:
socat -,raw,echo=0 tcp:127.0.0.1:2023
How to get the chisel on server? Download it or just compile from source in Dockerfile
download chisel
One probably root cause is the docker not running in detached mode on Heroku.
https://docs.docker.com/language/nodejs/run-containers/#run-in-detached-mode
Have someone do know how to activate -d option when the container is being executed by Heroku?

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

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'

rails server -d is not running as a background process

I currently have the below command running as a batch script
/usr/local/rvm/wrappers/ruby-1.8.7-p358/ruby /var/rails/sandbox/script/server -p 8080 -e sandbox -d
However the log ends at trying to initialize 0.0.0.0 8080
if I remove the -d so the script is
/usr/local/rvm/wrappers/ruby-1.8.7-p358/ruby /var/rails/sandbox/script/server -p 8080 -e sandbox
then everything runs perfect. The only issue is I am trying to detach the output so I can reuse the terminal session.
Please let me know if there is something I need to run?
Also just an fyi if I were to run the script/server alone I would have to use rvmsudo script/server -p 8080 -e sandbox -d, I get the same issue of it trying to end at 0.0.0.0 8080 but my site will not run and I will not get the message WEBrick::HTTPServer#start

Resources