Show Table of Contents
23.5. systemd ユニットファイルを使用した次回ブート時のジョブの実行スケジュール
cron、anacron、 at、および batch ユーティリティーを使うと、特定の時間に、またはシステム負荷が特定のレベルに達したときに、ジョブを実行するよう設定できます。また、次回のシステムブート時に実行するジョブを作成することもできます。これは、実行するスクリプトとその依存関係を指定する
systemd ユニットファイルを作成することで行います。
次回の起動時に実行するスクリプトを設定するには、以下を実行します。
- 起動プロセスのどの段階でスクリプトを実行するかを指定する
systemdユニットファイルを作成します。この例で挙げているのは、Wants=とAfter=の合理的な一連の依存関係を備えたユニットファイルです。~]#
cat /etc/systemd/system/one-time.service[Unit] # The script needs to execute after: # network interfaces are configured Wants=network-online.target After=network-online.target # all remote filesystems (NFS/_netdev) are mounted After=remote-fs.target # name (DNS) and user resolution from remote databases (AD/LDAP) are available After=nss-user-lookup.target nss-lookup.target # the system clock has synchronized After=time-sync.target [Service] Type=oneshot ExecStart=/usr/local/bin/foobar.sh [Install] WantedBy=multi-user.targetこの例を使用すると、以下が可能となります。/usr/local/bin/foobar.shを自分のスクリプトの名前に置き換えます。- 必要に応じて
After=エントリーのセットを変更します。
起動プロセスの段階を指定する方法については、「システムのユニットファイルの作成および変更」 を参照してください。 - スクリプトの実行後も
systemdサービスをアクティブに維持したいときは、RemainAfterExit=yes行を[Service]セクションに追加します。[Service] Type=oneshot RemainAfterExit=yes ExecStart=/usr/local/bin/foobar.sh systemdデーモンを再度読み込みます。~]#
systemctl daemon-reloadsystemdサービスを有効にします。~]#
systemctl enable one-time.service- スクリプトを作成し以下を実行します。
~]#
cat /usr/local/bin/foobar.sh#!/bin/bash touch /root/test_file - スクリプトを次回のブート時のみに実行したいときは、
systemdユニットを無効化する行を追加します。#!/bin/bash touch /root/test_file systemctl disable one-time.service - スクリプトを実行可能にします。
~]#
chmod +x /usr/local/bin/foobar.sh

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.