[英]How can I run a ruby script as a service including Sinatra in ubuntu?
I have written a script that parses my UPS's data via serial interface to a json file, looping indefinitely every 5 secs: 我编写了一个脚本,该脚本通过串行接口将UPS的数据解析为json文件,每5秒无限循环一次:
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: 我还有另一个小工具,可以使用sinatra创建一个小型api:
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. 我想在我的ubuntu服务器15.04中将它们都作为服务运行。 How do I keep the script running forever in the background as other ubuntu's services ? 与其他ubuntu服务一样,如何使脚本永远在后台运行? Should I also include sinatra in the service so it loads on boot ? 我是否还应该在服务中包含sinatra,以便在启动时加载?
You could start each service separately with upstart . 您可以使用upstart分别启动每个服务。
An upstart script lives in /etc/init
with extension .conf
, like /etc/init/myscript.conf
. 一个新贵脚本位于/etc/init
,扩展名为.conf
,例如/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
一旦完成,您可以使用start myscript
手动启动服务
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.