コンテンツにスキップ

systemd でサービスとして動かす

af start をフォアグラウンドで動かし続ける代わりに、systemd のユーザーサービスにします。 落ちたら再起動し、シェルを閉じても動き続けます。WSL2 では systemd が既定で有効です。

あなたがやること 起きること
unit ファイルを書く af start がユーザーサービスになる
systemctl --user enable --now で有効にする いますぐ起動し、ログインのたびに起動する
必要なら linger を有効にする WSL のセッションを閉じても動き続ける

~/.config/systemd/user/agent-fleet.service を次の内容で作ります。

[Unit]
Description=Agent Fleet (native)
[Service]
ExecStart=%h/.local/bin/af start
Restart=on-failure
# Lets the Console's "restart to apply" button (auto-update, below) find this
# unit; %N expands to the unit name (agent-fleet).
Environment=AF_SYSTEMD_UNIT=%N
[Install]
WantedBy=default.target
  • %h はホームディレクトリです。ExecStart のパスは、インストーラーで入れたとき(~/.local/bin/af のリンク)のものです
  • tar を手で展開したときは、展開した場所の af を指定します。例: ExecStart=%h/agent-fleet-native-<v>-linux-<arch>/af start。本当のパスは readlink -f "$(command -v af)" でわかります
  • AF_SYSTEMD_UNIT は、Console の「再起動して適用」がこの unit を見つけるためのものです(ネイティブパッケージを更新する)
  • 読み上げ・画像・エンジンを借りるための環境変数は、[Service] に Environment= の行で足します(例: Environment=AF_VOICEVOX_URL=...)

フォアグラウンドの af start がまだ動いていたら、先に止めます。動いていると、サービスがポート 8099 に bind できずに失敗します。

ターミナルウィンドウ
systemctl --user daemon-reload && systemctl --user enable --now agent-fleet
systemctl --user status agent-fleet # Active: active (running) と出れば OK

3. 閉じても動き続けるようにする

Section titled “3. 閉じても動き続けるようにする”

WSL のセッションを閉じたあとも動かしておくなら、linger を有効にします。

ターミナルウィンドウ
loginctl enable-linger "$USER"
ターミナルウィンドウ
journalctl --user -u agent-fleet -f
症状 見るところ
systemctl --user が Failed to connect to bus と出る ユーザーの systemd が動いていません。WSL2 では /etc/wsl.conf の [boot] に systemd=true を書き、wsl.exe --shutdown してから開き直します
サービスが起動してすぐ失敗する フォアグラウンドの af start が残っていて、ポート 8099 が使われていないか
ExecStart のコマンドが見つからない tar を手で展開したなら、readlink -f "$(command -v af)" のパスに書き換えます

エージェント向け

この指示をエージェントに送ると、このページを読ませられます。

https://agent-fleet.org/docs/ja/operate/native/service/index.md を読んで