0

I have made several jobs that god takes care of in my ruby application. However when the server reboots the job stops. I want to avoid this so I've made this script on my server. It looks like this.

my_app.sh

#!/bin/bash

# god tasks

#


case $1 in

start)

/usr/local/rvm/gems/ruby-1.9.3-p194/bin/god      
/usr/local/rvm/gems/ruby-1.9.3-p194/bin/god start
/usr/local/rvm/gems/ruby-1.9.3-p194/bin/god load /usr/local/Linux/apache2/www/hej.se/ruby/config/resque.god
/usr/local/rvm/gems/ruby-1.9.3-p194/bin/god load /usr/local/Linux/apache2/www/hej.se/ruby/config/resque_schedule.god

;;
esac

exit 0

If I log in manually and write

"/etc/init.d/my_app start"

it gives me

Sending 'start' command

No matching task or group
Sending 'load' command with action 'leave'

The following tasks were affected:
  resque-0
  resque-1
  resque-2
  resque-3
  resque-4
Sending 'load' command with action 'leave'

The following tasks were affected:
  resque_scheduler

And everything works, it does what I want it to do, i.e the jobs.

I have tried several ways to start this script on boot (Linux 10.4.4 LTS), rc.local, rc-default and now my latest attempt is crontab.

The script must be run under my user and not root, (it can't find the ruby installation if I run it under root).

Because of this I've configured the crontab under my user account:

@reboot /etc/init.d/my_app start

Sadly this doesn't work... I don't what I'm doing wrong. And this should probably not be necessary. I mean shouldn't you be able to this per auto when booting up the ruby application?

Im using passenger on this server, I don't know if this has something to do with it?

The solution below with the changes I made to the sh:

my_app.sh

bash -c "source /usr/local/rvm/scripts/rvm && /usr/local/rvm/gems/ruby-1.9.3-p194/bin/god"      
bash -c "source /usr/local/rvm/scripts/rvm && /usr/local/rvm/gems/ruby-1.9.3-p194/bin/god start"
bash -c "source /usr/local/rvm/scripts/rvm && /usr/local/rvm/gems/ruby-1.9.3-p194/bin/god load /usr/local/Linux/apache2/www/hej.se/ruby/config/resque.god"
bash -c "source /usr/local/rvm/scripts/rvm && /usr/local/rvm/gems/ruby-1.9.3-p194/bin/god load /usr/local/Linux/apache2/www/hej.se/ruby/config/resque_schedule.god"
Philip
  • 6,827
  • 13
  • 75
  • 104

1 Answers1

2

Forget the cronjob.

Centos/Fedora:

sudo chmod a+x /etc/init.d/my_app
sudo chkconfig --add my_app
sudo chkconfig my_app on

Ubuntu/Debian:

sudo update-rc.d my_app defaults

Both of these symlink the script to /etc/rc1.d, /etc/rc2.d, etc., and make the script available to run on boot for those runlevels.

bricker
  • 8,911
  • 2
  • 44
  • 54
  • How do I make the script run on boot? It can't find ruby when trying to run it under boot... "/usr/bin/env: ruby: No such file or directory" – Philip Oct 01 '12 at 08:25
  • The commands I described above will add it to the boot process. Check in /etc/rc*.d and those are all the scripts that automatically get run (their `start` command) for that runlevel (0-6). – bricker Oct 01 '12 at 08:33
  • If your ruby can't be found then you just need to figure out the paths. RVM tends to mess with that stuff and I generally don't recommend it on a production server, but since you already have it installed, you'll just need to figure out how to configure it to find ruby properly. – bricker Oct 01 '12 at 08:35
  • That was the trick! Wohoo! Thanks a bunch :) I edited my q with the solution. – Philip Oct 01 '12 at 08:48