How to automate startup of a web app with several independent processes? - ruby

I run the wesabe web app locally.
Each time I start it by opening separate shells to start the mysql server, java backend and rails frontend.
My question is, how could you automate this with a shell script or rake task?
I tried just listing the commands sequentially in a shell script (see below) but the later commands never run because each app server creates its own process that never 'returns' (until you quit the server).
I've looked into sub-shells and parallel rake tasks, but that's where I got stuck.
echo 'starting mysql'
mysqld_safe
echo 'starting pfc'
cd ~/wesabe/pfc
rails server -p 3001
echo 'starting brcm'
cd ~/wesabe/brcm-accounts-api
script/server
echo 'ok, go!'
open http://localhost:3001

If you don't mind the output being messed, put a "&" at the end of the line where you start the application to make it run in background.

Related

How can I run a Shell when booting up?

I am configuring an app at work which is on a Amazon Web Server.
To get the app running you have to run a shell called "Start.sh"
I want this to be done automatically after booting up the server
I have already tried with the following bash in the User Data section (Which runs on boot)
#!/bin/bash
cd "/home/ec2-user/app_name/"
sh Start.sh
echo "worked" > worked.txt
Thanks for the help
Scripts provided through User Data are only executed the first time the instance is started. (Officially, it is executed once per instance id.) This is done because the normal use-case is to install software, which should only be done once.
If you wish something to run on every boot, you could probably use the cloud-init once-per-boot feature:
Any scripts in the scripts/per-boot directory on the datasource will be run every time the system boots. Scripts will be run in alphabetical order.

Running background process (webserver) for just as long as other process (test suite) executes

I'm looking for a unix shell command that would allow me to run a process in the background (which happens to be a webserver) for just as long as another foreground process (which happens to be a test suite) executes. I.e, after the foreground process exits, the background process should exit as well. What I have so far is
.. preliminary work .. && (webserver & test)
which comes close, but fails in that the webserver process never exits. Is there a good way to express this in a single command or would it be more reasonable to write a more verbose script for that?
To give some further detail (and welcoming any relevant suggestions), I'm in the process of writing a javascript lib which I'd like to test using Selenium - hence the need for the webserver to execute 'alongside' the test suite. And in my attempt to do away with Grunt & co. and follow a leaner, npm-based approach to task management I've hit this shell-related road bump.
You can try this:
# .. preliminary work ..
webserver &
PID=$!
test_suite &
(wait $!; kill $PID)
This will work as long as you don't mind both commands being run in the background. What happens here is:
The webserver is started in the background.
The pid of it is assigned to $PID.
The test suite is started in the background.
The last line waits for the test suite to exit, and then kills the webserver.
If you need either command to be in the foreground, then run this version
# .. preliminary work ..
screen -dm -S webserver webserver
screen -dm -S test_suite test_suite
(wait "$(screen -ls | awk '/\.test_suite\t/ {print strtonum($1)}')"
screen -X -S webserver -p 0 kill) &
screen -r test_suite
Note that this requires the screen command (use apt-get install screen to install it or use ubuntu's website if you have ubuntu). What this does is:
Starts the webserver
Starts the test suite.
Waits for the test suite to exit, and then kills the webserver
In the meantime, resumes the test suite in screen

exit console after starting play framework application

I write a shell script for starting two play framework application.
cd /someDirectory
activator "start -Dhttp.port=9001"
cd /anotherDirectory
activator "start -Dhttp.port=9002"
after running the shell script, running process sleeps after starting first application (because play framework stay in shell after start command until pressing Ctrl + D)
How can I say to activator release shell after starting?
Use any process manager you want :
screen
nohup
upstart
systemd
supervisord
BTW, if this is production, you might consider using the start script created when packaging your application.
Use nohupas follows nohup ./script &. It is also useful when starting applications from remote shell.

Starting a Thin application via cron

I am running Shopify Dashboard on Centos 6 (http://shopify.github.io/dashing/). I wish to start this on boot and via a cron when I pull down an update from git.
I have the following code in a bash script which is the same code I run via the command line to start dashboard.
#!/bin/bash
cd /usr/share/dashboard/
dashing start -p 500 -d
running the actual script as the root user from the command line starts the application no problem.
However when this script is run via a cron or on boot then the application is never started.
If anyone could shed some light as to why this is the case it would most appreciated.
Per my comment I am still not 100% sure that the script is being run as root. I would add a line in the script:
echo $user > /tmp/test.txt
Then run the script via cron and see what the value of the file is.
Also I question your script. Why is it necessary to cd?
How about
/usr/share/dashboard/dashing start -p 500 -d
Also you may have to do a nohup, which is the no hang up signal, so ...
nohup /usr/share/dashboard/dashing start -p 500 -d
Those are my guesses.

Terminal Command: start or stop process if it's running or not

I have a glassfish install and I would like to have a simple terminal command checking if it's running. If it is, it will shutdown the process. Or if it not running, it will start the server.
To start the server I can enter:
/Applications/NetBeans/glassfish-3.1.1/bin/./asadmin start-domain domain
To stop the server I can enter:
/Applications/NetBeans/glassfish-3.1.1/bin/.asadmin stop-domain domain
I will would like to make a simple Alfred.app script than can start this domain if it's not running, or stop it if it's running.
One way to do this is to note in a file the process ID of the server when it starts, and have another script check to see if that process is running.
For example, script A (to run the server unconditionally):
#!/bin/sh
/Applications/NetBeans/glassfish-3.1.1/bin/./asadmin start-domain domain
# file name is arbitrary
pgrep whateverTheProcessNameIs > ~/.glassfish-server.pid
And in script B:
#!/bin/sh
pid=`pgrep -F ~/.glassfish-server.pid` # file chosen in script A
if [ "x$pid" = "x" ] ; then
# process has died; restart by running script A
/path/to/scriptA
fi
Note that only Mac OS X 10.8 (Mountain Lion) installs pgrep by default; otherwise you'd have to use some other method (like parsing ps output) to see what processes are running.
As far as how to run this check periodically, there are various ways. I'm assuming Alfred will run any script that is executable (chmod +x scriptA scriptB) but I don't know for sure.

Resources