Rails로 시작하는 시스템 시작
puma에서 - d 옵션 삭제
puma 쓰세요?
puma에서 -d 옵션이 사라진 지 1년이 넘었고(새로 온 사람이라 당시 상황을 몰라서) 지금까지 puma-daemon으로 시작했습니다.
하지만 오늘부터 시스템d로 시작하고 싶습니다.
도대체 왜 삭제됐냐면요.
deamon 옵션이 제대로 작동하지 않는 것 같습니다.
puma 명령을 실행한 후 데몬이 완료되기 전에 SIGHUP을 보내기 때문에 데몬이 중단되었습니다.
이런 느낌.
이 때문에 삭제되었지만 물론 많은 사람들이 이 명령을 즐겨 사용하기 때문에puma-daemon 같은 - d 옵션을 복구할 수 있다(실제로puma.rb에서는 demonaize 방법을 사용할 수 있다).
하지만 데몬화 같은 건 환경에 의존하기 때문에 각자 하는 게 좋을 것 같아요.
systemd를 사용해 보도록 하겠습니다.
필요한 것은 설정 파일과 sd-노티파이라는 GEM밖에 없으니까 편하게 해보세요.
프로파일 준비
/etc/systemd/system/puma.service
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
Type=notify
WatchdogSec=10
# 実行ユーザー
User=app-user
# 実行ディレクトリ
WorkingDirectory=/var/www
# 環境変数。必要なものを突っ込む
Environment="PUMA_DEBUG=1"
# Railsに必要なパス達の例。環境に合わせて調整しましょう。
Environment="PATH=/usr/bin:/usr/local/sbin:/root/.rbenv/shims:/root/.rbenv/bin:/root/.nvm/versions/node/v12.16.2/bin:/root/.local/bin:/root/bin"
# 起動コマンド
ExecStart=/home/app-user/.rbenv/shims/bundle exec puma -e production
Restart=always
[Install]
WantedBy=multi-user.target
GEM 설치
sd_notify는 시스템 d에 시작이 완료되었음을 알려야 합니다.각양각색의 언어가 존재하는 것 같다.
추가하지 않으면 start 명령은 영원히 끝나지 않습니다.
Gemfile
gem "sd_notify"
시작 명령
# 設定ファイルの再読み込み
systemctl daemon-reload
# 自動起動設定
systemctl enable puma.service
# 起動
systemctl start puma.service
# ステータス確認
systemctl status puma.service
# 再起動
systemctl restart puma.service
# 停止
systemctl stop puma.service
주의점 ①
시스템d에서 실행되는 경우 사용자를 지정해도bash_프로필 등을 읽지 않기 때문에, 필요한 것은 설정 파일의 '서비스에 써야 한다.
주의점 ②
환경 변수의 더블 쿼터 위치
NG는 감으로 할 수 있을 것 같았지만 제대로 진행되지 못했다.필요 없으면 묶지 않아도 돼.
NG
Environment=REDIS_PATH="hoge.fuga.ng.0001.apne1.cache.amazonaws.com"
OKEnvironment="REDIS_PATH=hoge.fuga.ng.0001.apne1.cache.amazonaws.com"
시동 걸어봐.
start->status->stop->status를 실행할 때의 출력입니다.
3
$ sudo systemctl start puma.service
$ sudo systemctl status puma.service
● puma.service - Puma HTTP Server
Loaded: loaded (/etc/systemd/system/puma.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2021-05-02 16:59:37 JST; 4s ago
Process: 5563 ExecStop=/bin/kill -TERM $MAINPID (code=exited, status=0/SUCCESS)
Main PID: 5570 (bundle)
CGroup: /system.slice/puma.service
├─5570 puma 5.2.2 (unix:///var/www/app/tmp/sockets/puma.sock) [app]
├─5600 puma: cluster worker 0: 5570 [app]
└─5601 puma: cluster worker 1: 5570 [app]
May 02 16:59:32 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5570]: [5570] * Max threads: 5
May 02 16:59:32 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5570]: [5570] * Environment: production
May 02 16:59:32 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5570]: [5570] * Master PID: 5570
May 02 16:59:32 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5570]: [5570] * Workers: 2
May 02 16:59:32 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5570]: [5570] * Restarts: (✔) hot (✔) phased
May 02 16:59:32 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5570]: [5570] * Listening on unix:///var/www/app/tmp/sockets/puma.sock
May 02 16:59:32 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5570]: [5570] Use Ctrl-C to stop
May 02 16:59:37 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5570]: [5570] - Worker 0 (PID: 5600) booted, phase: 0
May 02 16:59:37 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5570]: [5570] - Worker 1 (PID: 5601) booted, phase: 0
May 02 16:59:37 ip-10-1-100-152.ap-northeast-1.compute.internal systemd[1]: Started Puma HTTP Server.
$ sudo systemctl stop puma.service
$ sudo systemctl status puma.service
● puma.service - Puma HTTP Server
Loaded: loaded (/etc/systemd/system/puma.service; disabled; vendor preset: disabled)
Active: inactive (dead)
May 02 17:10:21 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5863]: [5863] * Listening on unix:///var/www/app/tmp/sockets/puma.sock
May 02 17:10:21 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5863]: [5863] Use Ctrl-C to stop
May 02 17:10:24 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5863]: [5863] - Worker 0 (PID: 5893) booted, phase: 0
May 02 17:10:24 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5863]: [5863] - Worker 1 (PID: 5894) booted, phase: 0
May 02 17:10:24 ip-10-1-100-152.ap-northeast-1.compute.internal systemd[1]: Started Puma HTTP Server.
May 02 17:10:33 ip-10-1-100-152.ap-northeast-1.compute.internal systemd[1]: Stopping Puma HTTP Server...
May 02 17:10:33 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5863]: [5863] === puma shutdown: 2021-05-02 17:10:33 +0900 ===
May 02 17:10:33 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5863]: [5863] - Goodbye!
May 02 17:10:33 ip-10-1-100-152.ap-northeast-1.compute.internal bundle[5863]: [5863] - Gracefully shutting down workers...
May 02 17:10:34 ip-10-1-100-152.ap-northeast-1.compute.internal systemd[1]: Stopped Puma HTTP Server.
순조롭게 진행되고 있다.잘 됐다!
총결산
평소에는 아무 생각 없이 시스템ctl start nginx에 간다.서비스 같은 걸 사용했지만 다소 이해가 돼서 좋았어요.
설정 파일을 몇 개 준비하면 환경에 따라 굿!
데몬의 어원
그나저나 데몬은 어떻게 된 걸까, 여신이 환생한 인큐버스 같은 걸 상상해왔고, 눈에는 그런 게 안 보이니까 수호수라고 생각했는데, 원래는'맥스웰의 악마'라는 물리학적 사고 실험에서 나왔다.
맥스웰의 악마를 간단명료하게 설명한 애니메이션은 재미있었다.
Reference
이 문제에 관하여(Rails로 시작하는 시스템 시작), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://zenn.dev/ymasutani/articles/ce42131f0e7b1a텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)