相关文章推荐
骑白马的毛衣  ·  JAVA ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Dear Stackoverflow community,

I am struggling with running a python script that executes a PyQt5 GUI on desktop startup of Raspberry Pi 3B with Raspbian Jessie.

What do I have so far?

  • Python script with shebang #!/usr/bin/env python3 in first line ( python3 --version is 3.4.2) running the GUI without any problems

  • Shell script (.sh) that is able to execute the GUI with the following lines:

    #!/bin/bash python3 GUI.py

  • Information that may help:

  • If I place both files in the same directory somewhere, the Shell script starts the GUI, but if they are on the desktop, it doesn't.

  • Automatic login to desktop is enabled.

  • Thank you in advance for any help.

    RaspiManu

    UPDATE:

    I solved my Problem with a lot of testing and posted an answer for other users.

    After a lot of testing, I figured it out myself. Here's how it worked for me...

    Create autorun-file:

    2.1 LXTerminal: cd /home/pi/.config/autostart

    2.2 LXTerminal: sudo nano pythonscript.desktop

    2.3 pythonscript.desktop:

    [Desktop Entry]
    Version=1.0
    Name=YourName
    Comment=Your comment
    Exec=/home/pi/pythonscript.py -nograb #-nograb for comboBox on touch Screen
    Icon=/usr/share/pixmaps/python.xpm
    Path=/home/pi/
    Terminal=false
    StartupNotify=true
    Type=Application
    Categories=Utility;Application;
    

    2.4 Ctrl+O, Ctrl+X, sudo reboot

    Good to know:

    It is important, that you can't use just any path to your script. The script has to be directly in the /home/pi/ directory, so you would use Exec=/home/pi/pythonscript.py in the autorun-file (.desktop). I also learned, that if your script loads for example an image with PIL, this image has to be somewhere else, maybe on your desktop, because it can't be opened out of the /home/pi/ directory.

    If your GUI has a comboBox and you are using a touch screen, the comboBox might make your whole GUI unuseable after you touched it. Using Exec=/home/pi/pythonscript.py -nograb solves this problem.

    StartupNotify=true is important for starting GUI scripts.

    Hope this helps,

    RaspiManu

    I am following your answer and able to run pyqt5 application on startup. But its not loading the images. My project directory is inside /home/pi/Documents/Project which contains all the images. Do I need to move my project to /hom/pi/ as per your suggestion – S Andrew Jul 19, 2020 at 6:12 Dear @SAndrew, this is a long time ago and I'm not completely sure. It's nice that your script runs on startup. When I did my GUI project, my script was located in /home/pi/ directory. The python code of this script opened pictures out of a folder on my desktop (Raspbian Stretch). Maybe you could try loading pictures from a desktop folder, too. If this works, you have a reference and you can experiment with other location options that might work. I really hope, this helps you out. Good luck :) – RaspiManu Jul 24, 2020 at 9:19 What am I doing wrong? cd /home/pi/.config/autostart gave me this error: -bash: cd: /home/pi/.config/autostart: No such file or directory – Phönix 64 Jul 14, 2021 at 22:34 Dear @Phönix64, this error means, that your system does not have that folder. The path to the autostart folder might have changed since 2018. As you can see in my original post, I worked with Raspbian Jessie which is outdated now. The system even had a whole name change from Raspbian to Raspberry Pi OS. I recommend you to do some research on the new location of the folder and try it again. Good luck with your project :) – RaspiManu Jul 18, 2021 at 9:29 Jup thank you though that it was some kind of configuration step I missed but turns out that they completely changed the service structure. Now It works – Phönix 64 Jul 18, 2021 at 10:12

    You can make a background service that runs on startup, by following this link Service method

    and By appending this line

    service yourdaemon start
    

    in /etc/rc.local

    assuming your service name is 'yourdaemon'

    Caution: Use the root previleges

    Example service file

    #! /bin/sh
    ### BEGIN INIT INFO
    # Provides:          yourdaemon
    # Required-Start:    $remote_fs $syslog
    # Required-Stop:     $remote_fs $syslog
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Your Daemon
    # Description:       Your Daemon
    ### END INIT INFO
    # PATH should only include /usr/* if it runs after the mountnfs.sh script
    PATH=/sbin:/usr/sbin:/bin:/usr/bin
    DESC="Your Daemon"
    NAME=yourdaemon
    DAEMON=/hannext/yourdaemon.py  # Path to your python file
    PIDFILE=/var/run/$NAME.pid
    SCRIPTNAME=/etc/init.d/$NAME
    LOGFILE=/var/log/snc/$NAME.log
    . /lib/lsb/init-functions
    do_start()
            echo "$(date +%F) $(date +%T) DAEMON   : Starting $DESC service" >> $LOGFILE
            start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --startas $DAEMON --make-pidfile --background
    do_stop()
            echo "$(date +%F) $(date +%T) DAEMON   : Stopping $DESC service" >> $LOGFILE
            start-stop-daemon --stop $DAEMON --quiet --oknodo --pidfile $PIDFILE
            rm -f $PIDFILE
    # Function that sends a SIGHUP to the daemon/service
    do_reload() {
            # If the daemon can reload its configuration without
            # restarting (for example, when it is sent a SIGHUP),
            # then implement that here.
            start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
            return 0
    case "$1" in
      start)
            [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
            do_start
            case "$?" in
                    0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                    2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
      stop)
            [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
            do_stop
            case "$?" in
                    0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                    2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
      status)
            status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
      #reload|force-reload)
            # If do_reload() is not implemented then leave this commented out
            # and leave 'force-reload' as an alias for 'restart'.
            #log_daemon_msg "Reloading $DESC" "$NAME"
            #do_reload
            #log_end_msg $?
      restart|force-reload)
            # If the "reload" option is implemented then remove the
            # 'force-reload' alias
            log_daemon_msg "Restarting $DESC" "$NAME"
            do_stop
            case "$?" in
                    do_start
                    case "$?" in
                            0) log_end_msg 0 ;;
                            1) log_end_msg 1 ;; # Old process is still running
                            *) log_end_msg 1 ;; # Failed to start
                    # Failed to stop
                    log_end_msg 1
            #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
            echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
            exit 3
    

    save it by the name of 'yourdaemon' in /etc/init.d/ and make it executable using

    chmod +x yourdaemon
                    Thank you very much for your answer and please excuse my question... I am very new to this and have never worked with daemons. I understand, that you can use the skeleton file in /etc/init.d for building one, but I looked into some other daemons like sudo or reboot and they seem to be written in C++ or C# which I am not able to use. Could you please explain the step of creating the daemon a bit more? And how do I use the root previleges?
    – RaspiManu
                    May 16, 2018 at 12:13
                    You absolutely made my day for helping me this much. Thank you! I am not at work anymore, but I will definitely try it tomorrow and tell you about it.
    – RaspiManu
                    May 16, 2018 at 15:00
                    Should $NAME stay as it is and use NAME=yourdemon, if this works like this, or do I have to change something more than yourdemon and the path to my .py file?
    – RaspiManu
                    May 16, 2018 at 15:08
                    it is the name of the service so you should name it accordingly. Please mark it as correct if you find it helpful , after your testing.
    – Axecalever
                    May 16, 2018 at 15:11
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.