title: how to daemonize Rails Rake task on Elastic Beanstalk start up
body: <p>I have an Elastic Beanstalk web server environment called "my-env".  I want my-env to execute the following bash command on start up:  <code>bundle exec rake jobs:work</code>.  I am new to EB and my Bash is mediocre (at best).  I flailed about all day yesterday and came up with the following which works, <code>.ebextensions/start_worker.config</code>:</p>
  
  <p><strong><em>Edit:</em></strong> Actually, the following isn't working correctly.  It seems to work properly initially then starts generating errors.  Perhaps the environment changes?</p>
  
  <pre><code>commands:
    create_post_dir:
      command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
      ignoreErrors: true
  files:
    "/usr/sbin/jobs_worker.sh":
      mode: "000755"
      owner: root
      group: root
      content: |
        #!/usr/bin/env bash
        . /opt/elasticbeanstalk/support/envvars
        export PATH=/home/ec2-user/.gem/ruby/2.1.2/bin:/opt/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/bin:/opt/rubies/ruby-2.1.2/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin
        cd $EB_CONFIG_APP_CURRENT
        su -c "RAILS_ENV=production bundle exec rake jobs:work" $EB_CONFIG_APP_USER
        echo "worker starting" &gt;&gt; /var/log/directory-hooks-executor.log    
    "/opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh":
      mode: "000755"
      owner: root
      group: root
      content: |
        #!/usr/bin/env bash
        nohup /usr/sbin/jobs_worker.sh 0&lt;&amp;- &amp;&gt; /var/log/directory-hooks-executor.log &amp;
  </code></pre>
  
  <p>So this creates a script in <code>/opt/elasticbeanstalk/hooks/appdeploy/post</code> so EB executes it automatically when I deploy.  I had to daemonize so that the script wouldn't hold up EB's start up sequence.</p>
  
  <p>So I doubt that this is going to work well in the long run.  I would appreciate any advice on how this could be done cleaner; perhaps by means of a linux service, or within a Rails initializer.  Also I would like to know if there is a danger that this script will be run more than once or stop unexpectedly.  So I guess I am asking for any general advise on how I could best proceed from here.</p>