ベスパリブ

プログラミングを主とした日記・備忘録です。ベスパ持ってないです。

systemdの'start request repeated to quickly, refusing to start'エラー

Raspberry Pi 3のRaspbian ver.8.0(Jessie)ではrc.localが非推奨となり、デフォルトで使用できなくなりました。
起動時にスクリプトを実行させたい場合、これからはsystemdを使いましょうとのことです。

systemdについての概要記事
http://equj65.net/tech/systemd-boot/

大体わかったような気がしたところで、とりあえず人のサンプルプログラムを動かしてみましょう。
http://qiita.com/DQNEO/items/0b5d0bc5d3cf407cb7ff
うまくいきました。

このサンプルプログラムを改造すればrc.localと同様の処理が実現できるというわけですね。少しずついじっていきましょう。少しずついじって…さっそくデーモンエラーが起きました。

$ systemctl -l status hello
● hello.service - hello daemon
   Loaded: loaded (/etc/systemd/system/hello.service; enabled)
   Active: failed (Result: start-limit) since 木 2017-01-26 14:58:38 JST; 2min 5
4s ago
 Main PID: 665 (code=exited, status=0/SUCCESS)

 1月 26 14:58:38 raspberrypi systemd[1]: hello.service holdoff time over, schedu
ling restart.
 1月 26 14:58:38 raspberrypi systemd[1]: Stopping hello daemon...
 1月 26 14:58:38 raspberrypi systemd[1]: Starting hello daemon...
 1月 26 14:58:38 raspberrypi systemd[1]: hello.service start request repeated to
o quickly, refusing to start.
 1月 26 14:58:38 raspberrypi systemd[1]: Failed to start hello daemon.
 1月 26 14:58:38 raspberrypi systemd[1]: Unit hello.service entered failed state
.

hello.service start request repeated too quickly, refusing to start.

「高速でリクエストを繰り返しすぎ」エラーのようです。

$ sudo vi /etc/systemd/system/hello.service
[Unit]
Descripton = hello daemon

[Service]
ExecStart = /opt/hello.sh
Restart = always
Type = simple

[Install]
WantedBy = multi-user.target

どうやら、Restart = alwaysが怪しいですね。
今、hello.shの中身は次のようになっています。

#!/bin/sh
echo Hello World >> /tmp/hello.log

このスクリプトが短すぎてすぐに処理が終わり、すぐにまたデーモンにリクエストがいっているせいでエラーが起きているようです。

Restart = noに書き換えてみましょう。

$ sudo vi /etc/systemd/system/hello.service
[Unit]
Descripton = hello daemon

[Service]
ExecStart = /opt/hello.sh
Restart = no
Type = simple

[Install]
WantedBy = multi-user.target

書き換えたら、hello.serviceを再度実行してみましょう。

$ sudo systemctl daemon-reload
$ sudo systemctl start hello
$ sudo systemctl status hello
● hello.service - hello daemon
   Loaded: loaded (/etc/systemd/system/hello.service; enabled)
   Active: inactive (dead) since 木 2017-01-26 15:44:25 JST; 2min 18s ago
  Process: 1006 ExecStart=/opt/hello.sh (code=exited, status=0/SUCCESS)
 Main PID: 1006 (code=exited, status=0/SUCCESS)

 1月 26 15:44:25 raspberrypi systemd[1]: Started hello daemon.

エラーが消えました。
hello.shを一回だけ実行したあと、inactiveになっています。
これでOKということで。