bb677a
Posts: 11
Joined: Mon Jan 02, 2017 9:38 pm

systemd autostart using PySide / PyQt

Wed Jul 19, 2017 12:32 pm

Hello,

I have a Python 2.7 script that works fine when starting it from a terminal. The script creates a GUI using PySide. I want the script to run automatically at every bootup, so I created a systemd service. The service creation itself works satisfactory when used with simple test scripts, but not with the script containing the GUI. The line of code that the script chokes on when started by the systemd service is:

app = QtGui.QApplication(sys.argv)

Thinking that the sys.argv may be an issue when not started in a terminal, I modified this line to:

app = QtGui.QApplication([r'/home/pi/Desktop/myScript.py'])

This still works starting from a terminal, but again, not when started as a service. Any ideas on how to get this to work? Thanks.

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: systemd autostart using PySide / PyQt

Thu Jul 20, 2017 1:39 pm

Does the service wait until the desktop is loaded to start the program ? If its a GUI program it needs to have the X-server to run/show on.
You have to tell the program which display ( X-server) to connect to. Easiest done with "export DISPLAY=:0.0" before starting the program.
The X-server is also protected from access for other users so if your program get started as root it will also be denied if the user currently logged in is "pi", see if you can change which user the service will run as. ( also a bossibility to use "su - pi -c /path/to/myprogram.py" in a script.)

Use the "journalctl" command to look at what your service prints as error message, it will give you (us) hints about where it fails.

Return to “Python”