DEV Community

Ahmad Ya'kob Ubaidullah
Ahmad Ya'kob Ubaidullah

Posted on

how to make rackup sinatra as systemd and auto restart on failure

sinatra app

#acc-server.rb
require 'sinatra/base'

class MyApp < Sinatra::Base
  set :bind, '0.0.0.0'
  set :port, 3333

  get '/' do
    "Hello #{params[:name]}"
  end
  run! if app_file == $0
end
Enter fullscreen mode Exit fullscreen mode

rackup

require './acc-server'
run MyApp
Enter fullscreen mode Exit fullscreen mode

shell script server.sh

#!/bin/bash
cd /root/server && /usr/local/bin/rackup -p 3333 -P /root/server/server.pid </dev/null &>>/root/server/log
Enter fullscreen mode Exit fullscreen mode

systemd conf /etc/systemd/system/pserver.service

[Unit]
Description=Prosody http server

[Service]
ExecStart=/root/server/server.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

start the service

systemctl start pserver.service 
Enter fullscreen mode Exit fullscreen mode

then enable it on startup

systemctl enable pserver.service 
Enter fullscreen mode Exit fullscreen mode

Discussion (0)