自宅サーバー ≫ Linux Server ≫ CentOS 7 ≫ 起動ファイルの作成

 
起動ファイルの作成
CentOS7 からの変更点 』でも書きましたが、各種サービス(アプリケーション等)の管理が systemd サービスに変更になり、起動用設定ファイルが /usr/lib/systemd/system/ に保存されています
yum でインストールされたものは自動で登録・保存されていますので systemctl start ****** 等で起動・停止・再起動・自動起動設定が行えますが、そうでないものは新しく設定・保存しなくてはなりません
そこで、すでに設定・保存されているファイルを参考に新しいファイルを作ってみたいと思います
ここでは web サーバーの設定ファイルを参考にDDNS自動更新ソフト『DiCE』の設定ファイルを作ってみます
まずは web サーバーの設定ファイルを確認します

 
 [root@centos]#
vi /usr/lib/systemd/system/httpd.service  
 [Unit]
   <--- Unitセクション
 Description=The Apache HTTP Server
   <--- Unit(サービス)の説明
 After=network.target remote-fs.target nss-lookup.target
   <--- このUnitより先に起動するべきUnit

 [Service]
   <--- Service セクション
 Type=notify
   <--- systemd のよって即時起動し利用可能になった時にデーモンが systemd に信号を送る
 EnvironmentFile=/etc/sysconfig/httpd
   <--- サービスのディレクトリ
 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
   <--- サービス起動コマンド
 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
   <--- サービスリロードコマンド
 ExecStop=/bin/kill -WINCH ${MAINPID}
   <--- サービス停止コマンド
 # We want systemd to give httpd some time to finish gracefully, but still want
 # it to kill httpd after TimeoutStopSec if something went wrong during the
 # graceful stop. Normally, Systemd sends SIGTERM signal right after the
 # ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
 # httpd time to finish.
 KillSignal=SIGCONT
   <--- 強制終了のシグナル
 PrivateTmp=true
   <--- セキュリティ問題解決するためサービス専用の独立した /tmp /var/tmp が作られる

 [Install]
   <--- Installセクション
 WantedBy=multi-user.target
   <--- enable時にこのUnitの.wantsディレクトリにリンクを作成
 

上記のような設定になっています
内容は [Unit] [Servisc] [Install] の3つのセクションから構成されています
 [Unit] セクション
オプション 内容
Description Unitの説明文
Documentation ドキュメントのURI
  Requires このUnitが必要とする前提Unit  前提Unitの起動に失敗した場合このUnitも起動できない
Wants このUnitが必要とする前提Unit  前提Unitの起動に失敗した場合でも無視しこのUnitを起動
After このUnitより先に起動するUnit
Before このUnitより後に起動するUnit
・記述例
オプション名=Unit・target名 Unit・target名
※複数指定する場合は半角スペースを挟んで指定
 [Servisc] セクション
オプション 内容
ExecStart サービス起動コマンド
ExecReload サービスリロードコマンド
ExecStop サービス停止コマンド
ExecStartPre/ExecStartPost サービス起動前後の追加コマンド(サービス起動判定には関連させたくないコマンドを記載)
ExecStopPost サービス停止後に実行するコマンド(サービスが異常停止した際にも実行される)
EnvironmentFile 環境変数を読み込むファイル
Type サービスプロセスの起動完了の判定方法(デフォルトは「simple」)
  PIDFile fork型サービスのメインプロセスのPIDファイル
BusName D-Bus型サービスのbus接続名
Restart サービスプロセス停止時の再起動条件(デフォルトは「no」)
PrivateTmp このサービス専用の/tmpと/var/tmpを用意する
・参考オプションコマンド
graceful 再読込
restart 再起動
/usr/bin/kill $MAINPID 終了
-HUP 設定ファイルの再読込とログファイルの更新
 [Install] セクション
オプション 内容
WantedBy enable時にこの Unit の .wants ディレクトリにリンクを作成する
RequiredBy enable時にこの Unit の .required ディレクトリにリンクを作成する
  Also enable/disable 時に同時に enable/disable する Unit
Alias enable 時にこの Unit の別名を用意
・記述例
オプション名=オプション.target オプション.target
※複数指定する場合は半角スペースを挟んで指定
上記 web サーバーの設定ファイルとセクション内容から DiCE のファイルを作ってみると下記のような感じになります
尚、起動・リロード・停止コマンドに dice のディレクトリを記述しただけ!!! それ以外は変更していません・・・・・(笑)

 
 [root@centos]#
vi /usr/lib/systemd/system/dice.service  
 
以下新規記述
 
[Unit]
 Description=The DiCE Server
   <--- ステータスで表示される名前
 After=network.target remote-fs.target nss-lookup.target

 [Service]
 Type=notify
 ExecStart=/usr/local/bin/DiCE/diced -d
   <--- dice の start コマンド
 ExecReload=/usr/local/bin/DiCE/diced $OPTIONS
   <--- reload の start コマンド
 ExecStop=/usr/local/bin/DiCE/diced $OPTIONS
   <--- dice の stop コマンド
 RemainAfterExit=yes
 PrivateTmp=true

 [Install]
 WantedBy=multi-user.target

 

ファイルが出来上がったらデーモンのリロード

 
 [root@centos]#
systemctl --system daemon-reload
 

DiCE の起動

 
 [root@centos]#
systemctl start dice.service
 

自動起動設定

 
 [root@centos]#
systemctl enable dice.service
 

自動起動設定の確認

 
 [root@centos]#
systemctl list-unit-files | grep dice.service
 dice.service                                  enable
   <--- enable になっていればOK
 

上記のように起動設定をした後、PCを再起動し DiCEが起動している事を下記のコマンドで確認

 
 [root@centos]#
ps -C diced
   PID   TTY         TIME CMD
  1144   ?       12:34:56 diced
   <--- 起動プロセス
 

上記のようにDiCEのプロセスが表示されればOK
一応、ステータスの確認

 
 [root@centos]#
systemctl status dice.service
 dice.service - The DiCE Server
    Loaded: loaded (/usr/lib/systemd/system/dice.service; enabled)
    Active: active (exited) since 水 2014-10-08 20:30:47 JST; 4min 53s ago
   Process: 4350 ExecStop=/usr/local/bin/DiCE/diced $OPTIONS (code=exited, status=0/SUCCESS)
   Process: 4399 ExecStart=/usr/local/bin/DiCE/diced -d (code=exited, status=0/SUCCESS)
  Main PID: 4399 (code=exited, status=0/SUCCESS)

 10月 08 20:30:47 papa-net.info systemd[1]: Unit dice.service entered failed state.
 10月 08 20:30:47 papa-net.info systemd[1]: Starting The DiCE Server...
 10月 08 20:30:47 papa-net.info systemd[1]: Started The DiCE Server.
 10月 08 20:30:47 papa-net.info diced[4399]: =-=-=- DiCE DynamicDNS Client -=-=-=
 10月 08 20:30:47 papa-net.info diced[4399]: Version 0.19 for Japanese
 10月 08 20:30:47 papa-net.info diced[4399]: Copyright(c) 2001 sarad
 10月 08 20:30:47 papa-net.info diced[4399]: DiCE Daemon Started !!
 Hint: Some lines were ellipsized, use -l to show in full.
 

戻る