Memo

メモ > サーバ > 各論: エトセトラ > バックグラウンドで処理を実行する(サービス)

■バックグラウンドで処理を実行する(サービス)
UNIXではバックグラウンドプロセス(常駐プログラム)を「デーモン」と呼ぶ daemonはギリシャ語由来の言葉で「精霊」「守護者」の意味。善でも悪でもない、目には見えない霊的存在を指す 転じて、バックグラウンドで動作して様々な雑務をこなしてくれるプロセスをデーモンと呼ぶようになった (なお、「悪魔」はdemonなのでスペルが異なる) 「サービス」も同じ意味で使われる 一例だが、以下のようにしてApacheのサービスを起動できる
# service httpd start
■nohup とデーモンの違い デーモンのざっくりした定義としては以下らしい 1. バックグランドで動作している 2. 制御端末を持たない 3. 他のプロセスグループに属さない 4. 親プロセスが init nohup は3・4を満たしてないので、デーモンとは言わないみたい systemd や supervisord などでサービス化されてるものはデーモンと呼んで良さそう supervisord については、前述の「バックグラウンドで処理を実行する(Supervisor)」を参照 nohup コマンド - Qiita https://qiita.com/sinsengumi/items/759ce48e844a46ed8091 unix - What's the difference between nohup and a daemon? - Stack Overflow https://stackoverflow.com/questions/958249/whats-the-difference-between-nohup-and-a-daemon ■サービスの起動方法が複数ある経緯 直接起動する場合
# /usr/sbin/apachectl start
他のサービスと同じような手順で扱えるようにするため、起動スクリプトが用意された 内部では /usr/sbin/apachectl を呼んでいる(/etc/rc.d/init.d/httpd をviなどで開けば、内容を確認できる)
# /etc/rc.d/init.d/httpd start
/etc/rc.d/init.d/ から /etc/init.d/ にシンボリックリンクが張られているので、以下のコマンドでも可能
# /etc/init.d/httpd start
さらに環境変数の影響を受けにくいようにした 内部では /etc/init.d/httpd を呼んでいる(/sbin/service をviなどで開けば、内容を確認できる)
# service httpd start
CentOS7からは、systemctlコマンドで行うようになった。serviceでも可能だが、systemctlコマンドにリダイレクトされる
# systemctl start httpd.service
apacheの起動について https://okwave.jp/qa/q1237460.html apachectlコマンドとhttpdの違い http://www.wegirls.tech/entry/2016/09/29/075200 デーモンの起動・終了にはserviceコマンドを利用しよう http://heartbeats.jp/hbblog/2013/06/service-start-stop.html 起動スクリプト(httpd)を読んでみた - まどろみの思考空間 http://hogespace.hatenablog.jp/entry/2013/06/13/222637 Systemdを使ってさくっと自作コマンドをサービス化してみる - Qiita https://qiita.com/DQNEO/items/0b5d0bc5d3cf407cb7ff ■設定を再読込 設定ファイルを編集して反映させたいだけなら、以下で十分かも。 restartでなければ反映されない項目もあるかも。要検証
# service httpd reload
■サービスを作成 実行すると「Hello World.」という文字を出力し続けるだけのプログラムを作成
# cd /opt/ # vi hello.sh
#!/bin/bash while true do echo "Hello World." >> /tmp/hello.log sleep 1 done
# chmod 0755 hello.sh # ./hello.sh
例えば以下のようにすると、バックグラウンドで処理できる
# nohup ./hello.sh & # ps aux | grep hello root 4030 0.0 0.2 113284 1408 pts/0 S 23:18 0:00 /bin/bash ./hello.sh root 4168 0.0 0.1 112812 972 pts/0 S+ 23:20 0:00 grep --color=auto hello $ kill 4030
このプログラムを、サービスとして起動する まずは以下に、Unit定義ファイルを作成する ファイル中にある「Restart = always」は、プロセスやサーバが不意に落ちた時に自動再起動するモード
# vi /etc/systemd/system/hello.service
[Unit] Description = hello daemon [Service] ExecStart = /opt/hello.sh … 実行したいファイルの場所を絶対パスで指定 Restart = always Type = simple [Install] WantedBy = multi-user.target
これでサービスを作成できた 以下、動作確認
# systemctl list-unit-files -t service | grep hello … サービスの状態を確認(Unitがサービスとして認識されたか確認) hello.service disabled # systemctl enable hello … サービスの自動起動を有効化 Created symlink from /etc/systemd/system/multi-user.target.wants/hello.service to /etc/systemd/system/hello.service. # systemctl list-unit-files -t service | grep hello … サービスの自動起動を確認 hello.service enabled # systemctl start hello … サービスを起動 # systemctl status hello … サービスの起動を確認 ● hello.service - hello daemon Loaded: loaded (/etc/systemd/system/hello.service; enabled; vendor preset: disabled) Active: active (running) since 日 2021-01-10 23:26:05 JST; 23s ago Main PID: 4274 (hello.sh) CGroup: /system.slice/hello.service ├─4274 /bin/bash /opt/hello.sh └─4310 sleep 1 1月 10 23:26:05 localhost.localdomain systemd[1]: Started hello daemon. # systemctl stop hello … サービスを停止 # systemctl disable hello … サービスの自動起動を無効化 # systemctl list-unit-files -t service | grep hello … サービスの自動起動の無効化を確認 hello.service disabled
Systemdを使ってさくっと自作コマンドをサービス化してみる - Qiita https://qiita.com/DQNEO/items/0b5d0bc5d3cf407cb7ff Linuxコマンド(Bash)でバックグラウンド実行する方法のまとめメモ - Qiita https://qiita.com/inosy22/items/341cfc589494b8211844 nohupをとデーモンの違いは何ですか? http://www.366service.com/jp/qa/295e6c772018443ae515774ff6d83538

Advertisement