Raspberry Pi 未启动 python 脚本在启动后等待输入
Raspberry Pi not start python script waiting for input just after startup
我在启动 raspberry pi 后启动 python 脚本时遇到问题。我尝试使用 init.d、rc.local 和 cron。没办法。
我的脚本等待输入并将其保存到文件:
import datetime
path = '/my/path/to/file.csv'
while 1:
name = input()
date = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
presence = str(name) + ";" + str(date) + '\n'
print(presence)
file = open(path, "a+")
file.write(presence)
file.close()
我怎么能运行启动后脚本会一直等待输入。
Cron:
sudo crontab -e
@reboot python /home/pi/Desktop/myscript.py
rc.local:
python /home/pi/Desktop/myscript.py
请注意,input()
从标准输入读取。从 init.d、rc.local 或 cron 启动的程序将在 /dev/null 上打开标准输入。这意味着 input()
将引发 EOFError。此外,input()
评估它读取的行。这可能不是你想要的。所以你的代码至少有两个问题。
由于您提供的信息不足,我无法提供解决方案。 "waiting for input all the time" 是什么意思?从哪里输入?如果输入产生连续的数据流,您真的希望 while
循环的主体 运行 尽可能快地执行吗?话虽如此,您可能想用简单的 sys.stdin.readline()
替换 input()
以避免隐式 eval()
.
我在启动 raspberry pi 后启动 python 脚本时遇到问题。我尝试使用 init.d、rc.local 和 cron。没办法。
我的脚本等待输入并将其保存到文件:
import datetime
path = '/my/path/to/file.csv'
while 1:
name = input()
date = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
presence = str(name) + ";" + str(date) + '\n'
print(presence)
file = open(path, "a+")
file.write(presence)
file.close()
我怎么能运行启动后脚本会一直等待输入。
Cron:
sudo crontab -e
@reboot python /home/pi/Desktop/myscript.py
rc.local:
python /home/pi/Desktop/myscript.py
请注意,input()
从标准输入读取。从 init.d、rc.local 或 cron 启动的程序将在 /dev/null 上打开标准输入。这意味着 input()
将引发 EOFError。此外,input()
评估它读取的行。这可能不是你想要的。所以你的代码至少有两个问题。
由于您提供的信息不足,我无法提供解决方案。 "waiting for input all the time" 是什么意思?从哪里输入?如果输入产生连续的数据流,您真的希望 while
循环的主体 运行 尽可能快地执行吗?话虽如此,您可能想用简单的 sys.stdin.readline()
替换 input()
以避免隐式 eval()
.