Jarno Jellesma
Jarno Jellesma

Reputation: 1

Python script on boot Linux

I would like to launch my python program (Graphical User Interface) on startup in Linux (Raspbian on a Raspberry PI).

I've made an initscript to launch my Python program, and I put it in the etc/init.d map.

I enabled it with the update-rc.d command. It all works fine.

But my Python script won't start with the following code in the initscript:

#!/bin/bash

### BEGIN INIT INFO
# Provides:          GUI
# Required-Start:    
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: This is a test daemon
# Description:       This is a test daemon
#                    This provides example about how to
#                    write a Init script.
### END INIT INFO



case $1 in
 start)
  python3 /home/pi/Desktop/GUI/GUI.py
  ;;
 stop)
  # Stop the daemon.
  if [ -e $PIDFILE ]; then
   status_of_proc -p $PIDFILE $DAEMON "Stoppping the $NAME process" && status="0" || status="$?"
   if [ "$status" = 0 ]; then
    start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
    /bin/rm -rf $PIDFILE
   fi
  else
   log_daemon_msg "$NAME process is not running"
   log_end_msg 0
  fi
  ;;
 restart)
  # Restart the daemon.
  $0 stop && sleep 2 && $0 start
  ;;
 status)
  # Check the status of the process.
  if [ -e $PIDFILE ]; then
   status_of_proc -p $PIDFILE $DAEMON "$NAME process" && exit 0 || exit $?
  else
   log_daemon_msg "$NAME Process is not running"
   log_end_msg 0
  fi
  ;;
 reload)
  # Reload the process. Basically sending some signal to a daemon to reload
  # it configurations.
  if [ -e $PIDFILE ]; then
   start-stop-daemon --stop --signal USR1 --quiet --pidfile $PIDFILE --name $NAME
   log_success_msg "$NAME process reloaded successfully"
  else
   log_failure_msg "$PIDFILE does not exists"
  fi
  ;;
 *)
  # For invalid arguments, print the usage message.
  echo "Usage: $0 {start|stop|restart|reload|status}"
  exit 2
  ;;
esac

Upvotes: 0

Views: 1352

Answers (1)

Vinicius Coque
Vinicius Coque

Reputation: 31

The problem is that when the init script run, there is not graphical interface available. Instead of using a init script, try configuring your application to run on X startup.

First add the command line to start your GUI app to ~/.xinitrc

# ~/.xinitrc

exec python3 /home/pi/Desktop/GUI/GUI.py

And then start the X server

startx

Upvotes: 1

Related Questions