アプリケーション開発ポータルサイト
ServerNote.NET
Amazon.co.jpでPC関連商品タイムセール開催中!
カテゴリー【Debian
Debian 10 Busterに/etc/rc.localを導入する
POSTED BY
2023-03-27

マシン起動時に実行したいスクリプト/etc/rc.localはBusterではデフォルト無効になっている。

/lib/systemd/system/rc-local.service

があるはずで、無ければ同ファイルを作成する。以下のようになっているはずである。

sourcerc-local.serviceGitHub Source
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

実行ファイル/etc/rc.localの作成

rc.local自体は無いのでイチから作成する。実行したいスクリプトを書く。

/etc/rc.local

sourcerc.localGitHub Source
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

su -l hogeuser -c whoami
su -l hogeuser -c "echo Hello World rc.local"

exit 0

su -lコマンドで任意の一般ユーザに成り代わり、-cコマンドでそれに続くコマンドを実行する。
上記例ではhogeuserとしてwhoami, echo Hello Worldと出力させている。
複数コマンドを一度に実行させたい場合は-cの先のコマンドをシェルスクリプトファイル等にする。
例)su -l hogeuser -c "/home/hogeuser/startup.sh"
など。

サービスとして登録する前に、まず単体で実行確認を行う。実行属性を必ずつけること。

chmod a+x /etc/rc.local
sh -x /etc/rc.local

+ su -l hogeuser -c whoami
hogeuser
+ su -l hogeuser -c echo Hello World rc.local
Hello World rc.local
+ exit 0

問題なさそうであるので、サービス登録・自動起動設定を行う。

systemctl status rc-local
systemctl start rc-local
systemctl stop rc-local
systemctl enable rc-local

マシンを再起動してみて、/var/log/syslogを確認してみる。

Oct 26 10:50:31 test-server systemd[1]: Starting /etc/rc.local Compatibility...
Oct 26 10:50:31 test-server rc.local[640]: hogeuser
Oct 26 10:50:31 test-server rc.local[640]: Hello World rc.local
Oct 26 10:50:31 test-server systemd[1]: Started /etc/rc.local Compatibility.

ちゃんとrc.localスクリプトが起動していることが確認できた。

※本記事は当サイト管理人の個人的な備忘録です。本記事の参照又は付随ソースコード利用後にいかなる損害が発生しても当サイト及び管理人は一切責任を負いません。
※本記事内容の無断転載を禁じます。
【WEBMASTER/管理人】
自営業プログラマーです。お仕事ください!
ご連絡は以下アドレスまでお願いします★

☆ServerNote.NETショッピング↓
ShoppingNote / Amazon.co.jp
☆お仲間ブログ↓
一人社長の不動産業務日誌
【キーワード検索】