簡體   English   中英

在 RPi 啟動時啟動時 Python 腳本不起作用

[英]Python script not working when launched at startup of RPi

帶 V1 凸輪的 RPi 1B。 當按下連接到 gpio 的按鈕時,Python 腳本會拍照。 然后圖片通過Mutt通過電子郵件發送。 一步一步做的時候一切正常。 但是在啟動時自動啟動時沒有按預期執行。

import subprocess
from datetime import datetime
from gpiozero import Button

button = Button(17)


while True:
        button.wait_for_press()
        time = datetime.now()
        filename = "capture-%04d%02d%02d-%02d%02d%02d.jpg" % (time.year, time.month, time.day, time.hour, time.minute, time.second)
        subprocess.call("raspistill -t 500 -o %s" % filename, shell=True)
        subprocess.call("echo "" | mutt -s 'Someone at the door' -i messageBody.txt myname@mailprovider.com -a %s" % filename, shell=True)

打字時一切正常:

$ python raspicam.py

我在幾秒鍾內收到一封附有圖片的精美電子郵件。

下一個合乎邏輯的步驟是讓這個腳本在啟動時啟動:

$ nano launcher.sh
#!/bin/sh
# launcher.sh
cd /
cd home/pi
python doorbell02.py
cd /

$ chmod 755 launcher.sh

$ sh launcher.sh

然后讓它在啟動時通過 cron 啟動:

$ mkdir logs

$ sudo crontab -e

添加:@reboot sh /home/pi/launcher.sh >/home/pi/logs/cronlog 2>&1

下次重啟時一切正常,除了通過 mutt 發送郵件。

$ ps aux顯示我的python 腳本和腳本啟動器屬於“root”...問題是從哪里來的?

root       475  0.0  0.0      0     0 ?        S    16:51   0:00 [cifsd]
root       500  0.0  0.6   7932  2300 ?        Ss   16:51   0:00 /usr/sbin/cron -f
root       502  0.0  0.6   9452  2384 ?        S    16:51   0:00 /usr/sbin/CRON -f
root       506  0.0  0.3   1924  1148 ?        Ss   16:51   0:00 /bin/sh -c sh /home/pi/launcher.sh >/home/pi/logs/cronlog 2>&1
root       511  0.0  0.2   1924  1108 ?        S    16:51   0:00 sh /home/pi/launcher.sh
root       513  1.5  2.5  34348  9728 ?        Sl   16:51   4:25 python doorbell02.py

我也無法讓 pdb 與我的腳本一起工作以獲取一些日志或調試信息......非常感謝一些提示非常感謝您的時間

嘗試在代碼中使用絕對路徑。 它在我的情況下幫助了我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM