2

On my Odroid running Ubuntu 16.04.3 LTS I have a python2 program that interacts with a display device (projector). When I run this program from the command line:

python ~/imgProc/torcam.py

everything works fine. I want this program to run at startup so I created a service file:

/lib/systemd/system/torcam.service

It contains the following:

[Unit]
Description=Torcam Service
After=rc-local.service network-online.target    
[Service]
User=root
ExecStart=/home/odroid/imgProc/starttor.sh
[Install]
WantedBy=multi-user.target

The startup script, starttor.sh contains:

#!/bin/bash
export DISPLAY=:0
cd /home/odroid/imgProc
python ./torcam.py

If I run this script from the command line everything works fine, but when I run this at boot or using systemctl I get an error saying "cannont open display: :0"

Here's how I installed the service:

odroid@odroid:~/imgProc$ sudo systemctl enable torcam
odroid@odroid:~/imgProc$ sudo systemctl start torcam
odroid@odroid:~/imgProc$ sudo systemctl status torcam

Here's the output:

odroid@odroid:~/imgProc$ sudo systemctl status torcam
● torcam.service - Torcam Service    
   Loaded: loaded (/lib/systemd/system/torcam.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2016-02-12 08:39:21 EST; 6min ago
   Process: 1807 ExecStart=/home/odroid/imgProc/starttor.sh (code=exited, status=1/FAILURE)
Main PID: 1807 (code=exited, status=1/FAILURE)

Feb 12 08:39:19 odroid systemd[1]: Started Torcam Service.
Feb 12 08:39:19 odroid systemd[1807]: torcam.service: Executing: /home/odroid/imgProc/starttor.sh
Feb 12 08:39:21 odroid starttor.sh[1807]: No protocol specified
Feb 12 08:39:21 odroid starttor.sh[1807]: (test:1808): Gtk-WARNING **: cannot open display: :0
Feb 12 08:39:21 odroid systemd[1]: torcam.service: Main process exited, code=exited, status=1/FAILURE
Feb 12 08:39:21 odroid systemd[1]: torcam.service: Unit entered failed state.
Feb 12 08:39:21 odroid systemd[1]: torcam.service: Failed with result 'exit-code'.

I understand that the problem is related to starting a service that interacts with the display, but I'm stuck on how to enable this so it works on boot. Any suggestions?

2 Answers 2

1

Your torcam needs an X server, and systemd scripts are not meant to use the X server (it may not even have started when the scripts are run).

If you need this program to access the display device/projector independent of the user logging in via keyboard/monitor, consider using two X servers (one for the monitor, one for the projector), and starting torcam with correct authentication (man xauth etc.) when the X server for the projector has started.

You'll need to read up on xorg.conf, and how to restrict the X server to just some outputs (assuming it's a single graphics card with multiple outputs). You'll also need to read up on how an X server is started (display manager like xdm etc., what you need for your monitor, vs. directly).

0

As kirkt mentioned, my problem was related to trying to use the X server running as root. After modifying my startup script, I was able to get the service to start correctly.

starrtor.sh:

#!/bin/bash
sleep 10
export DISPLAY=:0
cd /home/odroid/imgProc
exec sudo -u odroid /bin/sh - << eof
python ./torcam.py

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .