Dec 27

Hey all,
In Previous article i have given short bio of ruby daemon using daemon kit.
In this post i am writing about how to manage daemon,how to start/stop a daemon from any directory.

We can start a daemon using following way.
Go to the directory and give the command.

./bin/mydaemon

or

./bin/mydaemon start

(You know the difference right; To run it in foreground or background respectively.)

But consider this possibility, what if you didn’t want to go to the directory where the daemon is located and start it.

/home/mysystem/mydaemon/bin/mydaemon

or

/home/mysystem/mydaemon/bin/mydaemon start

Now you might be thinking why you will want to do that, let me say such conditions does arise (when you want monit to watch over it and restart it when it goes down.)

Usually this would work with ease if you are not using a bundler, however if you are using it then this would be something that you would get to see.

/usr/lib/ruby/gems/1.8/gems/bundler-1.0.15/lib/bundler/shared_helpers.rb:22:in `default_gemfile': Could not locate Gemfile (Bundler::GemfileNotFound)

This happens because bundler looks for the Gemfile in the directory you are in and the Gemfile is not there.
So what do you do to overcome it ?
You tell the daemon where to look for the gemfile
and how do you do it?

In you daemon place the following piece of code in bin/mydaemon at the very beginning.

ENV['BUNDLE_GEMFILE'] = "Path/to/your/daemon/Gemfile"

Now this is all fine and you have got your daemon working…. BUT what if others would also want to work with your daemon in there system.

This won’t work as the location given previously would not be the same as theirs.
Don’t worry I have a solution for that too.

Instead of

ENV['BUNDLE_GEMFILE'] = "Path/to/your/daemon/Gemfile"

put it as

ENV['BUNDLE_GEMFILE'] = File.join(File.dirname(__FILE__),'..','Gemfile')

This would help in anybody starting the daemon from any directory if required or starting the daemon by going to the directory and running it.

And thus you have your daemon running from your daemon root and other different location.
Hope this helps you at some point of time.

Be Sociable, Share!

Leave a Reply

preload preload preload