任意のコマンドをtelnet経由で呼び出せるようにする
/usr/local/bin/hello.sh に以下のような実行ファイル(コマンド)があるとする。
code:/usr/local/bin/hello.sh
#!/bin/sh
while :; do
printf 'Hello, World! : %s\n' "$(date)"
sleep 1
done
telnetでアクセスしたときにこれを実行したい。
/etc/systemd/system/hello.socket に以下の内容でファイルを作成する
code:/etc/systemd/system/hello.socket
Unit
Description=Hello Server (listener)
Socket
ListenStream=127.0.0.1:8383
Accept=yes
Install
WantedBy=sockets.target
/etc/systemd/system/hello@.service に以下の内容でファイルを作成する
*@.service というファイル名の、テンプレートユニットであることに注意
code:/etc/systemd/system/hello@.service
Unit
Description=Hello Server (implementation)
Service
Environment=LANG=C
ExecStart=/usr/local/bin/hello.sh
StandardInput=socket
StandardOutput=socket
StandardError=journal
Install
WantedBy=hello.socket
socketとserviceを有効化する
code:sh
systemctl enable hello.socket
systemctl enable hello@.service
socketを開始する
code:sh
systemctl start hello.socket
ローカルからtelnetコマンドでアクセスしてみる
code:sh
telnet 127.0.0.1 8383
参考:
systemdのsocketを使ってserverを作る - Qiita
systemd.socket
See Also:
systemd