How can I run a ruby script as a service including Sinatra in ubuntu? -


i have written script parses ups's data via serial interface json file, looping indefinitely every 5 secs:

require 'json'  pipe = io.popen("apcaccess")  upsdata_h = {} data = []  while true     pipe = io.popen("apcaccess")   upsdata_h[:ups] = {}   data = []    while (line = pipe.gets)     data = line.split(':')     upsdata_h[:ups][data[0].strip] = data[1].strip   end    puts "internal temperature: #{upsdata_h[:ups]['itemp']}"    file.open("upsdata.json", "w") |f|     f.write(upsdata_h.to_json)   end    sleep 5   end 

i have small 1 create tiny api sinatra:

require 'sinatra'  set :bind, '0.0.0.0'  '/api/upsdata'   content_type :json   file.read('upsdata.json') end 

i want run them both service in ubuntu server 15.04. how keep script running forever in background other ubuntu's services ? should include sinatra in service loads on boot ?

you start each service separately upstart.

an upstart script lives in /etc/init extension .conf, /etc/init/myscript.conf.

here's example simple upstart script:

#!upstart description "my server" author      "me"  start on filesystem stop on runlevel [!2345] respawn  script     /path/to/ruby /path/to/script end script 

once piece in place, can manually start service start myscript


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -