一時停止/再開のためのシステム単位ファイルの作成

一時停止/再開のためのシステム単位ファイルの作成

システムを起動するたびに、次のように入力します。

# su
Password:
# hciattach -s 152000 /dev/ttyS1 billionton

これは私のBTマウスを初期化するために必要なものです。一時停止から再開したら、次のように入力する必要があります。

# su
Password:

hciattach PIDを見つけて終了します。

# pkill hciattach
# hciattach -s 152000 /dev/ttyS1 billionton

これで2つのスクリプトを作成しました。

# cat bt-mouse-suspend.service 
[Unit]
Description=BT Mouse suspend helper
Before=sleep.target

[Service]
Type=simple
ExecStart=-/usr/bin/pkill hciattach

[Install]
WantedBy=sleep.target

# cat bt-mouse-resume.service 
[Unit]
Description=BT Mouse resume helper
After=suspend.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/hciattach -s 152000 /dev/ttyS1 billionton

[Install]
WantedBy=suspend.target
# systemctl status bt-mouse-resume
● bt-mouse-resume.service - BT Mouse resume helper
   Loaded: loaded (/etc/systemd/system/bt-mouse-resume.service; enabled; vendor preset: disabled)
   Active: active (exited) since Mon 2017-04-03 22:09:50 EEST; 12min ago
 Main PID: 6386 (code=exited, status=0/SUCCESS)
    Tasks: 0 (limit: 4915)
   CGroup: /system.slice/bt-mouse-resume.service

apr   03 22:09:50 Twinh systemd[1]: Started BT Mouse resume helper.
apr   03 22:09:51 Twinh hciattach[6386]: Device setup complete
# pgrep hciattach
#

ストップスクリプトは正常に動作し、予想される内容を終了します。しかし、履歴書hciattachは一度実行すると消えます。

ベストアンサー1

Afterあなたが話す内容が混乱して、Afterサービスが開始される前に他のデバイスを待つことになります。あなたWanted-By=suspend.targetAfter=suspend.targetあなたは互いに矛盾します。

Wanted-Byこれを宣言するのはbt-mouse-resume.serviceプロセスの一部ですが、suspend.target宣言Afterが完了するbt-mouse-resume.serviceまで待つ必要がありますsuspend.target。サービスはターゲットの一部ではありません。開始する前にそのターゲットを待つ必要があります。これはsuspend.target、シャットダウンではなく起動時にサービスを実行するように構成することを意味します。

あなたが本当に探しているのは、停止している間に何かを実行する方法なので、suspend.target次の中で非常に重要な部分をお知らせしますsystemd

順次依存関係を持つ2つのデバイスがシャットダウンすると、開始順序は逆になります。

引用する

だからあなたはあなたのものがうまくbt-mouse-suspend.serviceいくと言います。Wanted-By=sleep.targetこれはsleep.targetサービスの開始時に実行されます。逆にsleep.target止まるとbt-mouse-suspend.service止まります。このサービスにはフィールドは必要ありませんBefore。サービスをその宛先に対する操作にしました。

したがって、/usr/bin/hciattach -s 152000 /dev/ttyS1 billionton休暇をしたい場合。sleep.targetExecStopbt-mouse-suspend.service

また、これがどのように機能するかをよく読むことをお勧めしますsystemd。つまり、次を見てください。

https://www.freedesktop.org/software/systemd/man/systemd.service.html#

https://www.freedesktop.org/software/systemd/man/systemd.unit.html#

https://www.freedesktop.org/software/systemd/man/systemd.target.html#

また、2つのサービス目標suspend.targetはとですsleep.target。明らかに、実際に興味のあるものは何でも使うべきですが、ただsuspend.target

おすすめ記事