So I've written a daemon using the 'daemons' gem. I have essentially 3 pieces: daemon_controller => Controls startup/shutdown of daemon (in the style of the first example of the daemons documentation) daemonized_script => This script is the main program; it runs continuously in the background looking for work to do, and when it finds work, it forks itself and uses 'exec' to run a worker worker => processes the job given to it by the daemonized_script and exits The problem I'm having is that my worker program expects to be able to 'require' a few other classes I've written. It expects them to be in the same directory as it (or in a lib directory one dir up, whatever). The obvious issue here is that when the daemon_controller executes the daemonized_script as a daemon, it changes the directory to '/' and executes from there. I temporarily fixed this issue by putting in absolute paths in my source code to test and verify that everything else works as expected, which it does, so my question is this; how do I get a dynamic path set so that the worker process doesn't error when it can't find the files it needs? I can't use absolute paths because my program won't always be installed to the same directory, and anyway that's not a very flexible solution. I should also mention that all of these files live in the same directory structure, so relative paths should work fine. In other words, I'm not putting daemon_controller somewhere else in the filesystem and then trying to call my other files from there; they'll all be either in the same directory, or in close proximity to each other so that I only have change a directory up or down to get to the other files. Any response will be much appreciated.
on 2009-03-14 16:36
on 2009-03-14 23:27
I don't know if this is exactly what your looking for, but how about a call to locate? begin #worker code here rescue path = %x[locate <filename>].chomp! #the filename would be whatever the daemon is #looking for - you take the error code from #the daemon and pull the filename out of it #Here we are going to take the path output #then remove the last array element it's #split by, aka the filename, so that u will #have the directory directory = path.split("/") directory.pop directory.join("/") Dir.chdir(directory) retry #retries the begin block end
on 2009-03-14 23:36
Michael Linfield wrote: > I don't know if this is exactly what your looking for, but how about a > call to locate? > > begin > #worker code here > > rescue > path = %x[locate <filename>].chomp! > #the filename would be whatever the daemon is > #looking for - you take the error code from > #the daemon and pull the filename out of it > > #Here we are going to take the path output > #then remove the last array element it's > #split by, aka the filename, so that u will > #have the directory > > directory = path.split("/") > directory.pop > directory.join("/") > Dir.chdir(directory) > > retry #retries the begin block > > end My apologies, correct the line that says directory.join("/") to the following: directory = directory.join("/") Regards, - Mac
on 2009-03-16 13:51
On Mar 14, 6:33 pm, Michael Linfield <globyy3...@hotmail.com> wrote: > > #looking for - you take the error code from > > Dir.chdir(directory) > Regards, > > - Mac > -- > Posted viahttp://www.ruby-forum.com/. Thanks Michael. You're right, it's not exactly what I'm looking for, but it would probably work. I was looking more towards modifying my RUBYPATH environment variable to get this work; something that doesn't require putting in a bunch of special-case code inside of the source itself, especially above all of my "require" statements. I was thinking of writing a "locate_and_require" statement with your suggestions above if there really is no "right" way to do this. I've never written daemons before, so I don't really know how people generally handle this, and perhaps my expectations are out-of-whack. Anyway, I did find code that shows modifying of the $: variable inside a script to add/change your ruby path; however, it doesn't look like the changes survive daemonization. I'll keep looking around and post how I solved this once I get there.