I have written a script that parses my UPS's data via serial interface to a 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") do |f|
    f.write(upsdata_h.to_json)
  end

  sleep 5 

end

I have also another small one to create a tiny api with sinatra:

require 'sinatra'

set :bind, '0.0.0.0'

get '/api/upsdata' do
  content_type :json
  File.read('upsdata.json')
end

I want to run them both as a service in my ubuntu server 15.04. How do I keep the script running forever in the background as other ubuntu's services ? Should I also include sinatra in the service so it loads on boot ?

share|improve this question
up vote 2 down vote accepted

You could start each service separately with upstart.

An upstart script lives in /etc/init with extension .conf, like /etc/init/myscript.conf.

Here's an 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 this piece is in place, you can manually start the service with start myscript

share|improve this answer
    
It seems that upstart has been replaced with systemd in the latest ubuntu version. – Radolino Jun 26 '15 at 6:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.