/etc/rc.localに無限ループが表示されても、正常に起動できます。

/etc/rc.localに無限ループが表示されても、正常に起動できます。

無限に繰り返されるPythonスクリプトを入れましたが、/etc/rc.localコンピュータが正常に起動して混乱しています。

コンテンツ/etc/rc.local:

#!/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.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

/home/pi/py/startsignal.py &
/home/pi/py/fan.py
touch /home/pi/thisisrun
exit 0

signal.pyを起動

#!/usr/bin/python
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)

GPIO.output(18, 1)

fan.py

#!/usr/bin/python
# coding: utf8
import RPi.GPIO as gpio

gpio.setmode(gpio.BCM)
upper_temp = 55
lower_temp = 45
# minutes
check_interval = 2

def get_temp():
    with open('/sys/class/thermal/thermal_zone0/temp', 'r') as f:
        temp = float(f.read()) / 1000
    return temp

def check_temp():
    if get_temp() > upper_temp:
        gpio.setup(23, gpio.OUT)
    elif get_temp() < lower_temp:
        gpio.setup(23, gpio.IN)

if __name__ == '__main__':
    # check every 2 minutes
    try:
        while True:
            check_temp()
            sleep(check_interval * 60)
    finally:
        gpio.cleanup()

すべての関連コードは上記です。グーグルの終わりにこのような気がしました。

  1. #!/bin/sh -eエラーが発生すると、スクリプトが終了することを示します。
  2. ファイル/home/pi/thisisrunが生成されなかったため、この行にエラーがあるはずです。
  3. システムから起動した後、システムがfan.py実行されていることがわかります。だから実行中にこのようなエラーが発生していると思いますfan.py。しかし、fan.py無限ループが存在します!

Pythonスクリプトがエラーを生成しますが、まだ正常に実行できる方法は何ですか?

返されない/bin/shエラーをどのように検出しますかfan.py

オペレーティングシステム:Raspbian Stretch

ベストアンサー1

Raspbian Stretchがsystemd通常のDebian Stretchのようにデフォルトで使用されていると仮定すると、/etc/rc.local起動方法は次のとおりです/lib/systemd/system/rc-local.service

#  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
ConditionFileIsExecutable=/etc/rc.local
After=network.target

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

それが指定されているので、Type=forkingsystemdは基本的に起動して終了しても構いませんTimeoutSec=0RemainAfterExit=yesこれは、システムが/etc/rc.localまだ稼働しているにもかかわらず、ブートが正常に完了する理由を説明します。

スクリプトrc.localstartsignal.py最初にバックグラウンドで実行されます(=使用&)。つまり、この時点では、スクリプトの起動に失敗した場合にのみrc.localスクリプトにエラーが発生することを意味します。startsignal.py正常に開始されたがエラーが返されると、プロセスは着信エラーを読み取る必要がありrc.localます。しかし、あなたのプロセスは明らかにこれを確認することに興味がありません。wait <process or job ID>startsignal.py

これであなたの仕事がrc.local始まりますfan.py。なしで起動されるため、&シェルは実行するために別のプロセスを開始し、fan.pyそれが終了するのを待ちます...しかし、無限fan.pyループがあるため、システムがシャットダウンするfan.pyか、エラーが発生するか、プロセスが実行中になるまでシャットダウンされません。fan.py殺された。終了した後にのみ実行されますtouch /home/pi/thisisrunfan.py

始める方が合理的だと思いますstartsignal.py いいえ&fan.py そしてむしろその逆です。

おすすめ記事