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 ?