Systemdを使用して一時停止の前後にスクリプトを実行する

Systemdを使用して一時停止の前後にスクリプトを実行する

私が見つけたこのブログ投稿はここにあります。systemdを使用してサスペンドする前後にスクリプトを実行するプロセスについて説明します。次のように進んでください。

  1. スクリプトを生成します/usr/lib/systemd/system-sleep/suspend-date.sh
#!/bin/sh
if [ "${1}" == "pre" ]; then
  # Do the thing you want before suspend here, e.g.:
  echo "we are suspending at $(date)..." > /tmp/systemd_suspend_test
elif [ "${1}" == "post" ]; then
  # Do the thing you want after resume here, e.g.:
  echo "...and we are back from $(date)" >> /tmp/systemd_suspend_test
fi
  1. 実行権限を提供します。
chomd +x /usr/lib/systemd/system-sleep/suspend-date.sh
  1. コンピュータを一時停止する
  2. コンピュータを目覚めさせる
  3. 何が起こったかについてのログの表示
nano /tmp/systemd_suspend_test

しかし、ステップ5に達すると何も起こりません。ファイルsystemd_suspend_testが作成されたことがないため、表示内容はありません。

私のトラブルシューティング方法は次のとおりです。

  • スクリプトに適切な権限があることを確認してください。
[/usr/lib/systemd/system-sleep]$ ls -l
-rwxr-xr-x 1 root root 317 Sep 19 14:31 suspend-date.sh
  • 端末でコマンドを実行して動作していることを確認します。
echo "we are suspending at $(date)..." > /tmp/systemd_suspend_test && \
echo "...and we are back from $(date)" >> /tmp/systemd_suspend_test

/tmp/systemd_suspend_testファイルを表示します。

we are suspending at Wed 20 Sep 2023 02:25:28 PM EDT...
...and we are back from Wed 20 Sep 2023 02:25:28 PM EDT
  • 睡眠システムの再起動
$ sudo systemctl restart systemd-sleep
Failed to restart systemd-sleep.service: Unit systemd-sleep.service not found.

コンピュータがサスペンド状態に入ったときにsuspend-date.shスクリプトを起動(ディレクトリにあるかのように)するにはどうすればよいですか/usr/lib/systemd/system-sleep/

ベストアンサー1

おすすめ記事