簡體   English   中英

如何在RaspBerry Pi 3(Raspbian(Debian))中自動啟動python腳本?

[英]How autostart python script in RaspBerry Pi 3 (Raspbian (Debian))?

問題是我找不到系統啟動時自動執行的Application.py文件的方式。 我嘗試了幾種方法; crontab,init.d,rc.local。 然后執行了sh腳本,問題是它所在的行: sudo /usr/lib/python3 /home/pi/file.py ,什么都沒有發生。 我知道sh已執行,因為我創建了一個test1.log文件,並且始終在啟動時創建它。

另一方面,如果我手動執行sudo /etc/init.d/startApp start ,一切都會正常運行,並且當您將sudo /etc/init.d/inicioApp stop該過程將停止。 接下來,留下文件/etc/init.d/startApp ,看看是否有人可以幫助我,請記住Raspbian在Raspberry Pi 3 Model B V1.2上運行。

/etc/init.d/startApp文件:

#! /bin/sh
# /etc/init.d/startApp

### BEGIN INIT INFO
# Provides: startApp
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start Application
# Description: Start Application.py automatically.
### END INIT INFO


case "$1" in
 start)
  echo "Start Application.py"
  echo "startApp is running" >> /home/pi/test1.log
  sudo /usr/bin/python3 /home/pi/Application.py
  ;;

 stop)
  echo "Stop Application.py"
  /usr/bin/python3 -kill :1
  ;;

 *)
  echo "Usage: /etc/init.d/startApp {start|stop}"
  exit 1
  ;;
esac

exit 0

/home/pi/Application.py文件:

from PIL import Image, ImageTk
import sys

try:
    import tkinter as tk  # Python 3
except ImportError:
    import Tkinter as tk  # Python 2

import RPi.GPIO as GPIO

GPIO_present = True
GPIO_PULSE = 4
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(GPIO_PULSE, GPIO.IN, GPIO.PUD_UP)


class Application(tk.Frame):
    DELAY = 100  # ms
    IMG_DELAY = 200
    POST_DELAY = 2000

    def __init__(self, master=None):
        super().__init__(master)
        print("executed")
        self.pack()
        self.w, self.h = self.winfo_screenwidth(), self.winfo_screenheight()
        self.img_name = "/home/pi/pattern.png"
        self.control = 0
        self.create_widgets()
        self.flag = False
        self.after(self.DELAY, self.check_signal)
        root.bind('<Escape>', self.quitApp)

    def load_image(self, filename):

        try:
            pil_img = Image.open(filename)
            img_width, img_height = pil_img.size

            if img_width > self.w or img_height > self.h:  # Too big?
                size = (int(self.w), int(self.h))
                pil_img = pil_img.resize(size)
                ratio = min(self.w / img_width, self.h / img_height)
                img_width, img_height = int(img_width * ratio), int(img_height * ratio)
                pil_img = pil_img.resize((img_width, img_height), Image.ANTIALIAS)  # Resize.
            self.control = 0
            img = ImageTk.PhotoImage(pil_img)  # Convert to tkinter PhotoImage.
            return img

        except FileNotFoundError:
            self.control = 1

    def quitApp(self, a):
        root.destroy()

    def create_widgets(self):
        file = open("testfile.txt", "w")

        file.write("Se ejecuto.py")

        file.close()
        self.canvas = tk.Canvas(root, width=self.w, height=self.h, background='black')
        self.canvas.pack()

        self.pattern_img = self.load_image(self.img_name)
        if self.control == 1:
            sys.exit()
            root.destroy()

        self.image_id = self.canvas.create_image(self.w / 2, self.h / 2, image=None)

    def show_image(self):
        self.cur_img = self.pattern_img
        self.after(self.IMG_DELAY)
        self.canvas.itemconfigure(self.image_id, image=self.cur_img)
        self.update()
        self.after(self.POST_DELAY)
        self.canvas.delete(self.image_id)
        self.image_id = self.canvas.create_image(self.w / 2, self.h / 2, image=None)

    def check_signal(self):

        if GPIO_present:
            self.flag = not GPIO.input(GPIO_PULSE)

        if self.flag:
            self.show_image()
            self.flag = False  # Reset

        root.after(self.DELAY, self.check_signal)  # Check again after delay.


if __name__ == '__main__':
    root = tk.Tk()
    w, h = root.winfo_screenwidth(), root.winfo_screenheight()
    root.wm_attributes('-fullscreen', 'true')
    root.focus_set()

app = Application(root)

app.mainloop()

提前謝謝了!!!

我假設您正在運行raspbian,不是嗎?

由於Debian 7(基於Debian的Raspbian)默認的初始化系統是systemd ,因此與舊的已知systemv兼容,因此您仍然可以使用/etc/init.d/youscript方法,但請確保:

  • 授予您的init.d腳本startApp執行權限
  • 選擇您要執行startApp的運行級別,即5,3,依此類推,您可以使用以下命令找到默認運行級別:
sudo runlevel
  • 使用update-rc.d將您的腳本放入默認啟動級別的引導啟動中(這樣,您不必擔心默認值是/etc/rc5.d還是/etc/rc2.d,它會執行此操作。為了你 )
update-rc.d script-name default
  • 如果不使用update-rc.d,則為可選 )創建一個軟鏈接,並將其指向正確的運行級別目錄/etc/rcX.d,但要記住名稱!,這一點很重要,它將以'S'開頭,並且類似您希望在運行級別開始序列的最后執行“ 99”,即:
ln -s /etc/init.d/startApp /etc/rc5.d/S99startApp

暫無
暫無

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

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