问题
I have written a program that runs with ROOT permission in Terminal following login but fails when XOpenDisplay(NULL) call is made following reboot. The program is started up via rc.local but doesn't appear to be able to see X11.
I need ROOT because I make LibUSB calls and these fail if not ROOT. I can see the program is running with ROOT permission but inspection of environment variables shows DISPLAY not set. I have a delay loop running and checks are made after user login and user can see DISPLAY set in environment variables but program cannot. If program is terminated and run in Terminal it works perfectly so the problem is that the program cannot interact with X11 when it is started and this state persists.
I want the program to be up and running without user login.
Can anyone let me know what I need to do?
David
回答1:
Solved thanks to tripleee.
Ubuntu now uses LightDM and so by adding the line below to /etc/lightdm/lightdm.conf
greeter-setup-script=usr/bin/'my-script'
where 'my-script' is:
#!/bin/sh
/usr/bin/'my-prog' &
everything works.
回答2:
Your program should only start once X11 is up and running. But a much better design is to make a service which runs with root privileges, but doesn't use X, and arrange for an X client to talk to it somehow. If you cannot exist without X, hook into the X11 startup sequence, rather than the system startup.
来源:https://stackoverflow.com/questions/10517333/ubuntu-xopendisplaynull-fails-when-program-run-in-boot-sequence-via-rc-local