Start god process on server startup (Ubuntu) - ruby-on-rails

I'm currently struggling with executing a simple command which I know works when I run it manually when logged in as either root or non-root user:
god -c path/to/app/queue_worker.god
I'm trying to run this when the server starts (I'm running Ubuntu 12.04), and I've investigated adding it to /etc/rc.local just to see if it runs. I know I can add it to /etc/init.d and then use update-rc.d but as far as I understand it's basically the same thing.
My question is how I run this command after everything has booted up as clean as possible without any fuzz.
Im probably missing something in the lifecycle of how everything's initialized, but then I gladly encourage some education! Are there alternative ways or places of putting this command?
Thanks!

You could write a bash script to determine when Apache has started and then set it to run as a cron job at a set interval...
if [ "$(pidof apache)" ]
then
# process was found
else
# process not found
fi
Of course then you'll have a useless cron job running all the time, and you'll have to somehow flip a switch once it's run once so it doesn't run again.. This should give you an idea to start from..

Related

Script to automate timeshift backup and azuracast update

I’m running an Azuracast docker instance on Linode and want to try to find a way to automate my updates. Right now my routine is when I notice there are updates by accessing the Azuracast web panel, I usually run timeshift to create a backup using the following command
timeshift —-create —-comment “azuracast update ”
And then I use the following to update azuracast
cd /var/azuracast/
./docker.sh update-self
./docker.sh update
Then it asks me to ensure the azuracast installation is backed up before updating, to which i would usually just press enter.
After that is completed, it asks me if i want to clean up all stopped docker containers and images to save space, which i usually say no to.
What I’m wondering is if there is a way to create a bash script, or python or something to automate all of this, and then have it run on a schedule?
Sure, you can write a shell script to execute these commands and then run it on a schedule using crontab(5).
For example your script might look like:
#! /bin/sh
# Backup azuracast and restart docker container
timeshift --create --comment “azuracast update” && \
cd /var/azuracast/ && \
./docker.sh update-self && \
(yes | ./docker.sh update)
It sounds like this docker.sh program takes some user inputs. See if there are options you can pass to it that will allow you to run it non-interactively. (Seems there isn't, see edit.)
To setup your cron job, you can put the script in /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, or /etc/cron.monthly. Or if you need more control, you can get started configuring a cron job with crontab -e. Better explanation.
EDIT: Assuming this is the script you're using, it doesn't seem to have a way to run update non-interactively. Fear not though, there's a program for this: yes(1). This will answer yes to both of the questions, but honestly running docker system prune -f is probably a good idea. If you really want to answer no to that, you could probably substitute yes for printf "y\nn" to answer yes to the first and no to the second.
Also note that there's at least one other y/n question it could ask you, which you probably want to answer yes to.

Check if an app is deployed and undeploy it if it is deployed using Bash in Jenkins

I am using a job in Junkins to build my application (.ear) and then deploy it in Glassfish. I want to execute asadmin undeploy myApp before I deploy my application (the new version). The problem is in the 1st execution there is no application deployed so executing asadmin undeploy myApp generates an error. Any suggestion to deal with this situation. Any proposition is the most welcomed. Thank's.
EDIT :
Correct me if I am wrong in my method, maybe I am doing things wrong! Is this the right way to have a chain of production of a sowtware? Do I have to stop the server and restart it?
I never used Glassfish, but you could check if your app is deployed before to execute the undeploy.
If you know the port in which your app should be in execution, you could simply check with netstat or lsof.
EDIT:
Have a look to this doc (Example 2–3 Listing Applications), seems that you can see that with:
list-applications --type web
Regarding this:
Correct me if I am wrong in my method, maybe I am doing things wrong! Is this the right way to have a chain of production of a sowtware? Do I have to stop the server and restart it?
I think the correct answer is that it depends on the web server you are using (for example Glassfish provide the autodeploy). But generally, the approach works.
After watching some videos on Bash and with the help provided by Davide Patti, I figured out how to do it.
Knowing that I used the answer of Davide Patti and I thank him for his help I choosed to write my own answer for a simple reason: Patti's answer didn't work.
In order to test if an application is deployed and undeploy it if it is deployed I used the following Bash code which worked for me:
apps=`asadmin list-applications -t --user=admin --passwordfile=password.txt`
for app in $apps
do
if [ $app = "the_name_of_your_app" ]
then
asadmin --user=admin --passwordfile=password.txt undeploy the_name_of_your_app
fi
done;
PS: the content of password.txt is a single line: AS_ADMIN_PASSWORD=admin

Starting Erlang service at boot time (using Relx for creating release)

I have a server written in Erlang, compiled with Rebar, and I make a release with Relx. Starts nicely with
/root/rel/share3/bin/share3 start
The next step is to start when the server boots.
I have tried different approaches, the last one is using the /etc/init.d/skeleton where I changed the following
NAME=share3
DAEMON=/root/rel/share3/bin/share3
DAEMON_ARGS="$1"
After that, I run update-rc.d, but I have not gotten it too work. (Ubuntu 14.04)
The service runs until the machine reboots, and I need to login and start it again.
For Windows, it is really elegant, since it can create the Windows service.
Ubuntu uses upstart as init system, so you could try something like that:
description "Start my awesome service"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
exec /root/rel/share3/bin/share3
You have to place this script in /etc/init/ directory with '.conf' extension like '/etc/init/share3.coinf'. To start it invoke sudo start share3.
At last, I solved it!
I have told to relx to place the result at /home/mattias/rel. The script from relx is /home/mattias/rel/share3/bin/share3
Replace the row
SCRIPT_DIR="$(dirname "$0")"
by (you need to fix the path /home/mattias/rel)
HOME=/home/mattias
export HOME
SCRIPT_DIR="/home/mattias/rel/share3/bin"
Copy the file to /etc/init.d/share3 using
sudo cp ~/rel/share3/bin/share3 /etc/init.d/
Test that it works using
/etc/init.d/share3 start
and
/etc/init.d/share3 stop
In order to make it start at boot, install sysv-rc-conf
sudo apt-get install sysv-rc-conf
Enable boot at start using
sudo sysv-rc-conf share3 on
and disable
sudo sysv-rc-conf share3 off
Alternatives are welcome.

Run ruby script in background without using screen

I have a two scripts in the Rails environment which have to run 24/7. I'm working on a remote server, so I need to start the scripts using ssh, which means I need to keep the ssh windows open all the time.
I'm searching for a simple way to run these scripts in the background so they aren't canceled as soon as I close the ssh connection.
I don't want to use screen. I think there must be simpler way to handle this. Isn't there?
I think the most basic solution would be nohup:
nohup myscript &> /dev/null &
You can disown a script:
ruby script.rb &!
STDOUT/STDERR are still attached to the current shell, but the ruby process isn't a child of the shell anymore, so it won't get killed if you close the terminal.
Check Daemons. It's a toolkit for converting a script to a controllable daemon.
You can use
runit
supervisord
For daemonizing
Or some ruby stuff: https://www.ruby-toolbox.com/#Background_Processing for background processing

Run "passenger start" from within a shell script?

Basically, "passenger start" works fine from the terminal, but a simple script like "passenger start" doesn't work because it fails with:
*** Exception PhusionPassenger::UnknownError in PhusionPassenger::Rack::ApplicationSpawner (Could not find abstract-1.0.0 in any of the sources (Bundler::GemNotFound)) (process 19278, thread #<Thread:0x7f16dbfaf368>):
Looks to me like it can't find the gems anymore. This seems to be a very common problem on Google, but I cannot find any simple answer. I do not want to monkey patch my rails application with something like this: http://blog.ninjahideout.com/posts/the-path-to-better-rvm-and-passenger-integration (which I couldn't actually get to work, anyway)
It seems unbelievable that there's not a simple way to handle this. Why is running passenger from a shell script any different from typing it by hand?
UPDATE: basically, I fixed it by not using passenger. Instead of using "passenger start" I now use "rails server" and it works fine. Now, obviously, this doesn't "solve" the issue I was having, but it's good enough for my development needs.
I'd also like to elaborate a bit on my setup, because I think I was a bit vague.
Basically, I had this script called start_rails.sh
#!/bin/bash
cd /rails/app
passenger start
It didn't work, and I tried everything under the sun, including sourcing all of my bash files, and nothing worked. I changed it to:
#!/bin/bash
source "$HOME/.rvm/scripts/rvm"
cd /rails/app
rails s
And now it works. I can run it from an upstart script:
start on started mysql
exec sudo -u ubuntu -i /home/ubuntu/bin/start_rails.sh
However, "passenger start" still doesn't work, never has, and I guess never will =P I still don't know why there's no way to run a shell script and tell it to "run it exactly as if I was typing it in manually" because simply typing "passenger start" does work. From a shell script, it doesn't. Oh well. Life goes on.
If you can run passenger by hand without problems you are in a good position right now :).
I guess there is some problem with environment variables that are set when you log in but not when the script is run. Try to add these two lines at the beginning of your script:
source ~/.bash_profile
source ~/.bashrc

Resources