起動時にプログラムを自動実行

更新日: 2026.03.17

概要

以下の4つは、ラズベリーパイ起動時(CUI)に指定プログラムを自動実行させる代表的な方法

  1. /etc/rc.local
  2. .bashrc
  3. cron (crontab)
  4. systemd
"UV4Lで見守りカメラの製作" を例として、このアプリが持っているPythonのサーバ(~/monitor/monitor.py)を 自動起動させる方法を上記4つのケースに付いて説明します。

1. /etc/rc.local

概要

起動プロセスの最後に1回だけ自動実行されるシェルスクリプトです。実行のタイミングは

  1. カーネルの読み込み
  2. systemd (または init) の開始
  3. 各種システムサービスの起動
  4. /etc/rc.local の実行
  5. ログイン画面の表示

記述例

”/etc/rc.local” に実行したいコマンドやスクリプトを記述するのですが、記述条件に

  • #!/bin/bashで始まり、exit 0で終わる。
  • 実行は上記の #!/bin/bash と exit 0 間に記述。
  • パスはプルパスで指定
  が有ります。今回は、/home/ユーザー名/monitor/venv/bin/python3 で  /home/ユーザー名/monitor/monitor.py を自動起動させたい。 また "monitor.py" には下記の要求が有ります。
  1. "monitor.py" は Http_server を含み、カレントディレクトリを /home/ユーザー名/monitor/ としたい。
    • -> "cd" でカレントディレクトリを /home/ユーザー名/monitor/ に移動
  2. "monitor.py" は GPIOアクセスを含み、ルート権限で実行したい。
    • -> /etc/rc.local 自体が root権限で実行される。 sudo は必要無い。
  3. このプログラムでプロセスを止めない
    • -> バックグランドで実行
これらより ”/etc/rc.local" は以下の様になります。
#!/bin/bash cd /home/ユーザー名/monitor /home/ユーザー名/monitor/venv/bin/python3 monitor.py & (./venv/bin/python3 monitor.py & でも可) exit 0
仕上げに ”/etc/rc.local" に実行権を与えて sudo chmod +x /etc/rc.local 準備完了。

rc-localを有効にする

rc-localを有効にする為に "sudo systemctl start rc-local" を行ったところ

The unit files have no installation config (WantedBy=, RequiredBy=, Also=, Alias= settings in the [Install] section, and DefaultInstance= for template units). This means they are not meant to be enabled using systemctl. Possible reasons for having this kind of units are: • A unit may be statically enabled by being symlinked from another unit's .wants/ or .requires/ directory. • A unit's purpose may be to act as a helper for some other unit which has a requirement dependency on it. • A unit may be started when needed via activation (socket, path, timer, D-Bus, udev, scripted systemctl call, ...). • In case of template units, the unit is meant to be enabled with some instance name specified.

とエラーメッセージが出ました(OS:Bookworm)。 "rc-local" をどのタイミングで実行するか聞いています。 そこで "rc-local.service" を修正します。/lib/systemd/system/rc-local.service の 末尾に以下を追加

[Install] WantedBy=multi-user.target
sudo systemctl enable rc-local、 sudo systemctl start rc-local を実行後、リブートして下さい。 起動時にアプリが実行されます。

2. .bashrc

ターミナルを開くたびに実行される。設定は "~/.bashrc" に追加。

# ファイルを消す前に確認 alias rm='rm -i' # ll と打つだけで、ファイル詳細表示 alias ll='ls -laFh --color=auto' # monitor.pyを実行 alias gotest='cd /home/ユーザー名/monitor && /home/ユーザー名/monitor/venc/bin/python3 monitor.py'

これらはターミナルを開き、コマンドがタイプされた時に実行されるタイプ。 起動時には実行しません。下記の様に設定し

if [ "$(tty)" = "/dev/tty1" ]; then cd /home/ユーザー名/monitor sudo ./venv/bin/python3 monitor.py & fi
  • if [ "$(tty)" = "/dev/tty1" ]:モニターの有無に関わらずtty1 というメインの仮想端末を立ち上げる。
  • then 〜 :条件が合えば〜を実行
自動ログインに設定すれば、起動時に ”monitor.py” を実行出来ます。
  • 自動ログイン設定
    • sudo raspi-config
    • 1 System Options を選択
    • S6 Auto Login を選択
    • YesとOKを選択

3. cron (crontab)

指定した時間に、指定したコマンドを自動で実行する。 指定は5つの「時間指定」と「実行コマンド」の組み合わせです。 ”分 時 日 月 曜日 実行したいコマンド”

よく使う時間指定の例

設定意味
0 9 * * *毎日、朝の 9:00 に実行
*/10 * * * *10分おきに実行
0 0 * * 0毎週日曜日(0)の深夜 0:00 に実行
@rebootOSが起動した直後に1回だけ実行

設定は、crontab で行います。

  • crontab -e: 設定を編集します(初回はエディターを選択)。
  • crontab -l: 現在の設定内容を一覧表示します。
  • crontab -r: 全ての設定を削除します。
今回は @reboot sudo /home/ユーザー名/monitor/venv/bin/python3 /home/ユーザー名/monitor/monitor.py & を 一番下に追加。これで自動起動します。

4. systemd

現在の Linux における「最も正しく、強力な」自動起動の方法。 実行したいプログラムの情報を 「ユニットファイル」に書き込み、それをシステムに登録します。

設定ファイルの作成

  • 設定ファイルの作成:/etc/systemd/system/ に、拡張子 ”service” のファイルを作る。
  • 設定ファイルの中身
    [Unit] Description=My Python Test Project After=network.target [Service] # 実行するユーザーを指定(省略するとデフォルトで root になります) User=root Group=root # 作業ディレクトリ WorkingDirectory=/home/ユーザー名/monitor # 実行コマンド(フルパスで記述) ExecStart=/home/ユーザー名/monitor/venv/bin/python3 monitor.py # 落ちたら自動で再起動する設定 Restart=always [Install] WantedBy=multi-user.target
    • [Unit](ユニット): サービスの定義。
      • Description: サービスの名前や説明。systemctl status で表示される。
      • After: 起動の「順番」
    • [Service](サービス):実行内容を定義
      • ExecStart: 実行するコマンド。必ずフルパスで書く。デフォルトでバックグランドになる。
      • WorkingDirectory: プログラムが動く「作業場所」
      • Restart: systemd最大のメリット。Restart=always: プログラムが落ちても、「再起動」。
    • [Install](インストール):このサービスを起動するタイミングを定義します。
      • WantedBy=multi-user.target: After=network.targetの規定があるのでネットがつながって、マルチユーザーモードが上がったら起動となる
    このファイルを "monitor.service" として /etc/systemd/system/ 保存。

    設定ファイル有効化

    • 設定の読み込み: sudo systemctl daemon-reload
    • 自動起動の有効化: sudo systemctl enable monitor.service
    • サービスの開始: sudo systemctl start moniotor.service

最後に

自動起動サービスとしてどれを使用するか。

  • systemd:信頼性、機能を考慮してこれが本命
  • cron (@reboot):設定が簡単。次点。
  • .bashrc (tty1):自動起動はちょっと特殊。
  • rc.local:昔からの方法。最近のOSでは敬遠がち
って感じでしょうか。

SINCE 2026