title: How autostart python script in RaspBerry Pi 3 (Raspbian (Debian))?
body: <p>The problem is that I can not find the way for a Application.py file to auto-execute when the system starts. I have tried several ways; crontab, init.d, rc.local. And the sh script is executed, the problem is that the line where it is: <code>sudo /usr/lib/python3 /home/pi/file.py</code>, nothing happens. I know that the sh is executed, because I create a test1.log file and it is always created when starting. </p>
  
  <p>On the other hand, if I manually execute <code>sudo /etc/init.d/startApp start</code>, everything runs normally and when you put <code>sudo /etc/init.d/inicioApp stop</code> the process stops. Next I leave the file <code>/etc/init.d/startApp</code>, to see if someone can help me, remember that Raspbian is running on a Raspberry Pi 3 Model B V1.2.</p>
  
  <p>The <code>/etc/init.d/startApp</code> file:  </p>
  
  <pre><code>#! /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" &gt;&gt; /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
  </code></pre>
  
  <p>The <code>/home/pi/Application.py</code> file:</p>
  
  <pre><code>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('&lt;Escape&gt;', self.quitApp)
  
      def load_image(self, filename):
  
          try:
              pil_img = Image.open(filename)
              img_width, img_height = pil_img.size
  
              if img_width &gt; self.w or img_height &gt; 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()
  </code></pre>
  
  <p>Many thanks in advance!!!</p>