Skip to content

Instantly share code, notes, and snippets.

@dux
Last active February 15, 2022 17:22
Show Gist options
  • Save dux/050324229ece27281c83626ab3c9ff3f to your computer and use it in GitHub Desktop.
Save dux/050324229ece27281c83626ab3c9ff3f to your computer and use it in GitHub Desktop.
linux services systemd
# list all services
sudo systemctl --type=service | tee
# work with particular service
# status / start / stop / restart
sudo systemctl status app | tee
# see service log
sudo journalctl -u app.service
# if things go south
sudo systemctl daemon-reload
sudo systemctl reset-failed
# /etc/systemd/system/app-puma.service
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
Type=simple
User=deployer
WorkingDirectory=/home/deployer/app
ExecStart=/home/deployer/.rbenv/shims/puma -C /home/deployer/app/config/puma.rb
PIDFile=/home/deployer/app/tmp/puma.pid
Restart=always
[Install]
WantedBy=multi-user.target
# caddy.service
# /lib/systemd/system/caddy.service
# See https://caddyserver.com/docs/install for instructions.
[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target
[Service]
Type=notify
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
# https://caddy.community/t/serving-tens-of-thousands-of-domains-over-https-with-caddy/11179
{
admin off
# on_demand_tls {
# ask http://localhost:3000
# interval 2m
# burst 5
# }
}
(static) {
@static {
path /assets/*
path *.ico *.css *.js *.gif *.jpg *.jpeg *.png *.svg *.woff *.woff2 *.json
}
header @static Cache-Control max-age=5184000
}
:443 {
encode gzip
import static
tls {
on_demand
}
route {
file_server /assets/* {
root /home/deployer/app/public
}
reverse_proxy http://localhost:3000
}
}
# set app root
Dir.chdir File.expand_path("../..", __FILE__)
# load RACK_ENV
require 'dotenv'
Dotenv.load
require 'bundler/setup'
Bundler.require
plugin :tmp_restart # restart on touch of tmp/restart.txt
port 3000
log_requests false
nakayoshi_fork true
pidfile './tmp/puma.pid'
state_path './tmp/puma.state'
# activate_control_app('tcp://127.0.0.1:9000', no_token: true)
if ENV['RACK_ENV'] == 'production'
stdout_redirect './log/puma.log', './log/puma_errros.log'
environment 'production'
threads 32, 32
# this kills ruby Sequel and producess erros
# https://stackoverflow.com/a/22100819/254915
# preload_app!
# and this must be 0
workers 0
# restart if there is no response on /ok
Thread.new do
loop do
sleep 10
unless `curl -si -m 5 http://localhost:3000/ok`.include?('200 OK')
Logger.new('log/app_boot.log').error 'http://localhost:3000/ok fail, restarting'
`touch tmp/restart.txt`
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment