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: sudo /usr/lib/python3 /home/pi/file.py
, nothing happens. I know that the sh is executed, because I create a test1.log file and it is always created when starting.
On the other hand, if I manually execute sudo /etc/init.d/startApp start
, everything runs normally and when you put sudo /etc/init.d/inicioApp stop
the process stops. Next I leave the file /etc/init.d/startApp
, to see if someone can help me, remember that Raspbian is running on a Raspberry Pi 3 Model B V1.2.
The /etc/init.d/startApp
file:
#! /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
The /home/pi/Application.py
file:
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()
Many thanks in advance!!!
I assume you are running raspbian isn't it?
Since Debian 7 (raspbian is based on Debian) the default init system is systemd , nonetheless there is compatibility with the old known systemv , so you can still use the /etc/init.d/youscript approach but please make sure to:
sudo runlevel
update-rc.d script-name default
ln -s /etc/init.d/startApp /etc/rc5.d/S99startApp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.