Movatterモバイル変換


[0]ホーム

URL:


LoginSignup
8

Go to list of users who liked

5

Share on X(Twitter)

Share on Facebook

Add to Hatena Bookmark

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

systemd の systemctlコマンドでインスタンスを使用して引数を渡す方法

Posted at

systemctlコマンドでインスタンスを使用して引数を渡す方法を紹介します。

環境

  • OS:CentOS Linux release 7.7.1908

1. Unit定義ファイルを作成

以下のUnit定義ファイルを作成する。

  • サービス名(ここではtest) +@ +.serviceとする。
  • %iを使用する。
/etc/systemd/system/test@.service
[Unit]Description=TestService[Service]ExecStart=/bin/echo %I

2. systemctlコマンドで引数(インスタンス名)を指定して起動

systemctl start test@"arg1 arg2"

[root@CENTOS7 ~]# systemctl start test@"arg1 arg2"[root@CENTOS7 ~]#

journalctl -u test@"arg1 arg2"で確認。

[root@CENTOS7 ~]# journalctl -u test@"arg1 arg2"-- Logs begin at 日 2019-12-29 17:19:15 JST, end at 日 2019-12-29 20:06:21 JST.12月 29 20:06:21 CENTOS7 systemd[1]: Started TestService.12月 29 20:06:21 CENTOS7 echo[1602]: arg1 arg2

/etc/systemd/system/test@.service%Iではなく%iを使用すると、arg1arg2の間の半角スペースがエスケープされ、arg1\x20arg2となってしまう。

参考

ユニットファイルがロードされる際に置換されるユニット指定子は以下となります。

ユニット指定子意味詳細
%nフルユニット名
%Nフルユニット名(エスケープなし)%nと同じだが、文字列がエスケープされない。
%p接頭辞インスタンスになったユニット名の中の "@" 文字の前の文字列を参照する。
そうでないユニットの場合、接尾辞を除いたユニット名が使用される。
%P接頭辞(エスケープなし)%pと同じだが、文字列がエスケープされない。
%iインスタンス名インスタンスになったユニット名の"@" 文字と接尾辞の間の文字列を使用する。
%Iインスタンス名(エスケープなし)%iと同じだが、文字列がエスケープされない。
%fファイル名(エスケープなし)エスケープされていないインスタンス名。
%cユニットのコントロールグループこのグループ名には、/sys/fs/cgroup/systemd/の接頭辞が含まれない。
%rコントロールグループの親グループのパス"%c"の親のcgroupパスが使用される。
%Rスライスとユニットが置かれるルートコントロールグループパスシステムインスタンスの場合、「/」になる。
なおコンテナの場合、コンテナのルートコントロールグループパスを使用する。
%tランタイムディレクトリ/run又は、$XDG_RUNTIME_DIRが使用される。
%uユーザ名マネージャーを実行しているユーザのユーザ名が使用される。
通常はroot。
%UユーザのUIDマネージャーを実行しているユーザのUIDが使用される。
システム管理者の場合は0が使用される。
%hユーザのホームディレクトリマネージャーを実行しているユーザのホームディレクトリが指定される。
システム管理者の場合は、「/root」が使用される。
%sユーザのシェルサービスマネージャーが使用しているシェル。
システム管理者の場合は、「/bin/sh」が使用される。
%mマシンIDシステムのマシンID。
%bBoot IDシステムのブートID。
%Hホスト名ユニットがロードされた時点でのシステムホスト名。
%vkernelのリリースバージョン「uname -r」の出力で取得できる文字列を使用。

以上

8

Go to list of users who liked

5
0

Go to list of comments

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8

Go to list of users who liked

5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?


[8]ページ先頭

©2009-2025 Movatter.jp