0

I want to run python PyGame script on the Raspberry Pi at startup as early as it's possible. The script itself depends on PyGame and PySerial modules.

To achieve desired result I decided to wrap python script to a service and create a configuration for the systemd.

But unfortunately, script isn't running properly in these conditions. The execution hangs on pygame.display.init() line. Even when after full boot procedure I'm doing service restart via systemctl - behaviour is not changing in any way.

I'm creating a service first time, so there may be some really obvious mistake that I made, but can you please tell me what's wrong here and why I can't run this script successfully in the systemd?

Here is my shrinked main.py:

import os
import pygame

if __name__ == "__main__":
    print 'Starting IC app ...'
    print 'Environment variables:'
    print os.environ
    print 'Starting pygame initialization'
    pygame.display.init()
    print 'pygame display module initialized'
    pygame.font.init()
    print 'pygame font module initialized'

And here is the .service file:

[Unit]
Description=IC service
StartLimitIntervalSec=0
Before=dhcpcd.service
Wants=dhcpcd.service

[Service]
Type=simple
Restart=on-abort
RestartSec=1
SendSIGHUP=no
User=root
Environment="NO_AT_BRIDGE=1" "PYTHONUNBUFFERED=1" "DISPLAY=:0"
ExecStart=/usr/bin/python /home/pi/ic/src/main.py
StandardOutput=journal+console
StandardError=journal+console

[Install]
WantedBy=multi-user.target

Here is the output of the script when run via SSH as root (runs as expected):

root@raspberrypi:/home/pi/ic/src# python main.py 
Starting IC app ...
Environment variables:
{'NO_AT_BRIDGE': '1', 'LOGNAME': 'root', 'USER': 'root', 'HOME': '/root', 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', 'DISPLAY': ':0', 'LANG': 'en_GB.UTF-8', 'TERM': 'xterm', 'SHELL': '/bin/bash', 'SHLVL': '1', 'SUDO_USER': 'pi', 'USERNAME': 'root', 'SUDO_UID': '1000', 'SDL_VIDEO_X11_WMCLASS': 'main.py', '_': '/usr/bin/python', 'SUDO_COMMAND': '/bin/su', 'SUDO_GID': '1000', 'PWD': '/home/pi/ic/src', 'MAIL': '/var/mail/root'}
Starting pygame initialization
pygame display module initialized
pygame font module initialized

Here is the output of the script, grabbed from journalctl:

systemd[1]: Started IC service.
python[1217]: Starting IC app ...
python[1217]: Environment variables:
python[1217]: {'LANG': 'en_GB.UTF-8', 'NO_AT_BRIDGE': '1', 'TERM': 'linux', 'SHELL': '/bin/sh', 'JOURNAL_STREAM': '8:17484', 'SDL_VIDEO_X11_WMCLASS': 'main.py', 'LOGNAME': 'root', 'USER': 'root', 'HOME': '/root', 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', 'PYTHONUNBUFFERED': '1', 'DISPLAY': ':0', 'INVOCATION_ID': '2e02b3153baf4ab9be43b66157980bcb'}
python[1217]: Starting pygame initialization

Visual diff against environment variables during runs (left is SSH, right is systemd) enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.