spuder
spuder

Reputation: 18407

How do you make a ruby script start at boot?

I've just written my one of my first ruby scripts. Its purpose is to run a command on a server whenever it gets called from a git web hook.

require 'webrick'

server = WEBrick::HTTPServer.new(Port: ARGV.first)
server.mount_proc '/' do |req, res|
  r10kstatus = system( "sudo r10k deploy environment -pv warn" )
  puts r10kstatus
end

trap 'INT' do server.shutdown end
server.start

Now that I have a working script, how do I 'deploy' it.

I need to:

I assume I could greate a gem, however how do I manage starting the script automatically?

Options I'm looking into:

fpm
pleaserun
rubygems

Update

I've created a rubygem which I can deploy with puppet.

  package {'r10k_gitlab_webhook':
    ensure   => latest,
    provider => gem,
  }

http://rubygems.org/gems/r10k_gitlab_webhook

I'm still not sure how to make it so this script is executed everytime the server is booted

Upvotes: 0

Views: 936

Answers (3)

spuder
spuder

Reputation: 18407

The solution that works for me is to use pleaserun.

Here is how I did it:

# Assumes ruby > 1.9, and r10k_gitlab_webhook is in path
gem install pleaserun
gem install r10k_gitlab_webhook
pleaserun  --install --user git --group git --description 'Starts webserver on port 8000' r10k_gitlab_webhook 8000

This creates the following file in /etc/init/r10k_gitlab_webhook.conf.
Note, that this is a centos specific init file. If you run pleaserun on ubuntu it will create an upstart file instead.

description     "Starts webserver on port 8000"
start on filesystem or runlevel [2345]
stop on runlevel [!2345]

respawn
umask 022
#nice
#chroot /
#chdir /
#limit core <softlimit> <hardlimit>
#limit cpu <softlimit> <hardlimit>
#limit data <softlimit> <hardlimit>
#limit fsize <softlimit> <hardlimit>
#limit memlock <softlimit> <hardlimit>
#limit msgqueue <softlimit> <hardlimit>
#limit nice <softlimit> <hardlimit>
#limit nofile <softlimit> <hardlimit>
#limit nproc <softlimit> <hardlimit>
#limit rss <softlimit> <hardlimit>
#limit rtprio <softlimit> <hardlimit>
#limit sigpending <softlimit> <hardlimit>
#limit stack <softlimit> <hardlimit>


exec chroot --userspec git:git / r10k_gitlab_webhook "8000"

The service will now be started on every boot.

Upvotes: 1

Schylar
Schylar

Reputation: 774

In my company, when my boss needs one of my scripts ran at the bootup we just put the script in the startup folder but I'm not for sure if that'd work for you. Another thing to give a shot would be task scheduler. Just set the trigger. Point it towards your ruby script and bam. Good luck

Upvotes: 0

Felix
Felix

Reputation: 4716

I successfully use fpm (https://github.com/jordansissel/fpm) to create debian packages from ruby stuff. I would not use it for larger scale tasks, but its perfectly fine for (my) in-house stories.

Then, get into rc.d/init and figure out how to start it at a specific runlevel. This depends on the distribution you are using (assuming it is a unixoid system).

My standard route would be to install an apache, phusion passenger and then create a sinatra/rack-based application to do the job and let apache handle the "autostart" and "webrick" part (its not webrick anymore).

But I choose this because the machines in question in my scenario run apache anyway and the tools around are part of my "toolbelt". Imho it is a valid solution, if you plan to use similar techniques anyway.

If I remember correctly, the thin webserver has a install command to register itself, too.

Upvotes: 1

Related Questions