サービス失敗時にザオリクでは復活しないのでsystemdに頼る

サービス失敗時にsystemdでサービスを自動的に再起動したい. デフォルトの場合

$sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since 火 2017-10-10 10:10:22 JST; 6min ago
 Main PID: 25194 (nginx)
    Tasks: 2
   Memory: 1.7M
      CPU: 14ms
   CGroup: /system.slice/nginx.service
           ├─25194 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           └─25195 nginx: worker process

サービスのプロセスを殺す.

$sudo kill 25194

息をしていません.

$sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since 火 2017-10-10 10:17:16 JST; 3s ago
  Process: 25248 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited
 Main PID: 25194 (code=exited, status=0/SUCCESS)

再度,復活させて.

$sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since 火 2017-10-10 10:00:01 JST; 2min 54s ago
  Process: 25098 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS
 Main PID: 24702 (nginx)
    Tasks: 2
   Memory: 3.8M
      CPU: 22ms
   CGroup: /system.slice/nginx.service
           ├─24702 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           └─25101 nginx: worker process

/lib/systemd/system/nginx.serviceを開きます. [Service]に以下の2行を追加する.

Restart=always
RestartSec=3

デーモンをリロードする

sudo systemctl daemon-reload

サービスを再起動して

$sudo systemctl restart nginx

PIDを確認する

$sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since 火 2017-10-10 10:07:54 JST; 7s ago
  Process: 25160 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited
  Process: 25167 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 25163 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 25170 (nginx)
    Tasks: 2
   Memory: 1.7M
      CPU: 13ms
   CGroup: /system.slice/nginx.service
           ├─25170 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           └─25171 nginx: worker process

$ ps -ef|grep nginx
root     25170     1  0 10:07 ?        00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;

プロセスをkillします

$sudo kill 25170

プロセスを再確認

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since 火 2017-10-10 10:10:22 JST; 1min 5s ago
  Process: 25183 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited
  Process: 25191 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 25187 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 25194 (nginx)
    Tasks: 2
   Memory: 1.8M
      CPU: 14ms
   CGroup: /system.slice/nginx.service
           ├─25194 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           └─25195 nginx: worker process

$ps -ef|grep nginx
root     25194     1  0 10:10 ?        00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;

プロセスの再起動が表示されました.自動ザオリクです. お疲れさまでした.