0

I would like to executea script as root whenever I boot the system. I wrote a shell script:

# cat /home/root/demo_wrap.sh 
#!/bin/bash
`/home/root/demo_start.sh` > /dev/null &

that works great when I invoke it from the shell. I wrote the following service file: (Updated according to comments from Edgar Magallon):

# cat /etc/systemd/system/demo_start.service 
[Unit]
Description=starts the MH11 demo on EVK

[Service]
Environment="DISPLAY=:0"
Type=forking
XAUTHORITY=/home/root/.Xauthority
ExecStart=/home/root/demo_wrap.sh

[Install]
WantedBy=graphical.target

(I have also tried with DISPLAY:1 s well as XAUTHORITY=/run/user/0 with no success)

but for some reason, # systemctl start demo_start will not invoke the application but report:

[[0;1;31m*[[0m demo_start.service - starts the MH11 demo on EVK
     Loaded: loaded (/etc/systemd/system/demo_start.service; enabled; vendor preset: disabled)
     Active: [[0;1;31mfailed[[0m (Result: exit-code) since Wed 2022-05-11 18:58:25 UTC; 1min 11s ago
    Process: 251 ExecStart=/home/root/demo_wrap.sh (code=exited, status=0/SUCCESS)
   Main PID: 252 (code=exited, status=127)

May 11 18:58:18 smarc-rzv2l systemd[1]: Starting starts the MH11 demo on EVK...
May 11 18:58:18 smarc-rzv2l systemd[1]: Started starts the MH11 demo on EVK.
May 11 18:58:25 smarc-rzv2l demo_wrap.sh[254]: error: XDG_RUNTIME_DIR not set in the environment.
May 11 18:58:25 smarc-rzv2l demo_wrap.sh[254]: Connecting to display server failed!: No such file or directory
May 11 18:58:25 smarc-rzv2l demo_wrap.sh[254]: [ERROR] Wayland::init failed
May 11 18:58:25 smarc-rzv2l demo_wrap.sh[252]: /home/root/demo_wrap.sh: line 2: RZ/V2L: No such file or directory
May 11 18:58:25 smarc-rzv2l systemd[1]: [[0;1;39m[[0;1;31m[[0;1;39mdemo_start.service: Main process exited, code=exited, status=127/n/a[[0m
May 11 18:58:25 smarc-rzv2l systemd[1]: [[0;1;38;5;185m[[0;1;39m[[0;1;38;5;185mdemo_start.service: Failed with result 'exit-code'.[[0m 

While export XDG_RUNTIME_DIR="/run/user/0" is set in /etc/profile:

export XDG_RUNTIME_DIR=/run/user/$UID

and the weston display manager launches and displays properly on the HDMI screen.

ENVIRONMENT:

This is on an embedded platform with a Yocto built distribution that runs the Weston compositor. When I start a shell window in Weston and type echo $DISPLAY, it does not print anything I expected this to be :0 or :1 (per Edgar Magallon, below). As the terminal window appears prperly on the Weston desktop. This is an embedded reference platform and only has one user root. It is not connected to a network nor will anybody have access to it by connecting any other means of hardware to it. Security is not a concern!

I found the following on the system:

# cat /lib/systemd/system/weston\@.service 
[Unit]
Description=Weston Wayland Compositor
RequiresMountsFor=/run
Conflicts=plymouth-quit.service
After=systemd-user-sessions.service plymouth-quit-wait.service

[Service]
User=%i
PAMName=login
EnvironmentFile=-/etc/default/weston
StandardError=journal
PermissionsStartOnly=true
IgnoreSIGPIPE=no

ExecStart=/usr/bin/weston-start -v -e -- $OPTARGS

and my demo should be started after this but it's a bit odd: # systemctl status weston.service shows me:

[[0;1;32m*[[0m weston.service
     Loaded: loaded (/etc/init.d/weston; generated)
     Active: [[0;1;32mactive (exited)[[0m since Wed 2022-05-11 18:57:23 UTC; 33min ago
       Docs: man:systemd-sysv-generator(8)
    Process: 189 ExecStart=/etc/init.d/weston start (code=exited, status=0/SUCCESS)

May 11 18:57:22 smarc-rzv2l systemd[1]: Starting weston.service...
May 11 18:57:23 smarc-rzv2l systemd[1]: Started weston.service.
May 11 18:57:34 smarc-rzv2l weston[196]: Connecting to display server failed!: No such file or directory
May 11 18:57:34 smarc-rzv2l weston[196]: [ERROR] Wayland::init failed
May 11 18:57:35 smarc-rzv2l weston[190]: /etc/profile.d/zz1-demo_wrap.sh: line 2: RZ/V2L: No such file or directory

while Weston is up & running fine.

6
  • Since your script depends on GUI apps you should add under [Service] section this: Environment="DISPLAY=:0" "XAUTHORITY=/home/youruser/.Xauthority maybe /run/user/userID instead of /home/youruser/.Xauthority Commented Dec 20, 2022 at 2:03
  • Btw some GUI apps don't allow or do recommend that running them should be avoided. So if you are not able to run the gui app you should run the service as another user. For that you can add the line User=someuser in the [Service] section. Commented Dec 20, 2022 at 2:08
  • If you have some error about DISPLAY variable try changing DISPLAY=:0 to DISPLAY=:1. I see you are using Wayland and unless in my case that uses the value :1 instead of :0 for the DISPLAY Commented Dec 20, 2022 at 2:12
  • Regarding the second comment: don't allow or do recommend that running them as root should be avoided Commented Dec 20, 2022 at 4:19
  • @EdgarMagallon I've updated my post above, also see the update in the ENVIRONMENT: section Commented Dec 20, 2022 at 19:55

1 Answer 1

1

What happens is that your service file is trying to execute your application before other services have been started. You need to add to your demo_start.service the directive After that tells the service to run only if the other services are already running. Given the fact that you need the Weston compositor you must add the weston.service to the directive.

Also, regarding the XDG_RUNTIME_DIR error, the same logic applies. For this case you need to add the systemd-user-sessions.service so the /run/user/0 directory is created before trying to execute your application. Besides that, you need to set to the directive Environment the path to the XDG_RUNTIME_DIR directory.

Your demo_start.service file should have the following directives:

[Unit]
Description = starts the MH11 demo on EVK
After = weston.service systemd-user-sessions.service

[Service]
User = root
Environment = "XDG_RUNTIME_DIR=/run/user/0"
ExecStart = /home/root/demo_wrap.sh

[Install]
WantedBy = multi-user.target

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.