I'm trying to run a Pygame script on startup on Linux with a systemd service.
Here's my service file named: test.service:
[Unit]
Description=Test
Wants=systemd-logind.service systemd-user-sessions.service display-manager.service
After=systemd-logind.service systemd-user-sessions.service display-manager.service
[Service]
ExecStart=/usr/bin/python3 /home/enws/Desktop/test/main.py
[Install]
WantedBy=graphical.target
When I try to start the service with sudo systemctl start test, nothing happens and I see the logs with sudo systemctl status test, I get:
ā test.service - Test
Loaded: loaded (/etc/systemd/system/test.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2022-09-15 16:03:31 +03; 586ms ago
Process: 26454 ExecStart=/usr/bin/python3 /home/enws/Desktop/test/main.py (code=exited, status=1/FAILURE)
Main PID: 26454 (code=exited, status=1/FAILURE)
Eyl 15 16:03:30 dasdasdasdasd systemd[1]: Started Test.
Eyl 15 16:03:31 dasdasdasdasd python3[26454]: ALSA lib pcm_dmix.c:1052:(snd_pcm_dmix_open) unable to open slave
Eyl 15 16:03:31 dasdasdasdasd python3[26454]: pygame 2.1.2 (SDL 2.0.16, Python 3.6.9)
Eyl 15 16:03:31 dasdasdasdasd python3[26454]: Hello from the pygame community. https://www.pygame.org/contribute.html
Eyl 15 16:03:31 dasdasdasdasd python3[26454]: Traceback (most recent call last):
Eyl 15 16:03:31 dasdasdasdasd python3[26454]: File "/home/enws/Desktop/test/main.py", line 8, in <module>
Eyl 15 16:03:31 dasdasdasdasd python3[26454]: window_surface = pygame.display.set_mode((0, 0))
Eyl 15 16:03:31 dasdasdasdasd python3[26454]: pygame.error: No available video device
Eyl 15 16:03:31 dasdasdasdasd systemd[1]: test.service: Main process exited, code=exited, status=1/FAILURE
Eyl 15 16:03:31 dasdasdasdasd systemd[1]: test.service: Failed with result 'exit-code'.
My main.py is from the pygame_gui docs:
import pygame
import pygame_gui
pygame.init()
pygame.display.set_caption('Quick Start')
window_surface = pygame.display.set_mode((0, 0))
background = pygame.Surface((800, 600))
background.fill(pygame.Color('#000000'))
manager = pygame_gui.UIManager((800, 600))
hello_button = pygame_gui.elements.UIButton(relative_rect=pygame.Rect((350, 275), (100, 50)),
text='Say Hello',
manager=manager)
clock = pygame.time.Clock()
is_running = True
while is_running:
time_delta = clock.tick(60)/1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
if event.type == pygame_gui.UI_BUTTON_PRESSED:
if event.ui_element == hello_button:
print('Hello World!')
manager.process_events(event)
manager.update(time_delta)
window_surface.blit(background, (0, 0))
manager.draw_ui(window_surface)
pygame.display.update()