Reputation: 1467
I have a C++ program that takes the screenshot of my Ubuntu 14.04 machine. The program uses the X11 library. I need to execute this application from a daemon at boot time. But it returns XOpenDisplay failed
. I think it is because the X11 server is not loaded. Is there any programmatic way by which I can get the screenshot soon after the X11 server is loaded?
EDIT:
Missed to tell something.. The daemon gets started at boot time and it fails to take screenshot. So after starting I stopped the daemon and started it again from terminal. The same error(XOpenDisplay failed) is shown then.. Is it a problem with screenshot and daemon??
Upvotes: 1
Views: 1974
Reputation: 238351
Add a script in /etc/X11/Xsession.d/
. The script will be run when X11 is running.
If you really need to start the daemon at boot, then what you could do, is send a signal from the script to the daemon process. You'll need to store the pid somewhere when you start the daemon.
kill -SIGUSR1 $PID
Then, your daemon should at the start register a signal handler that will be called when the signal is received.
Taking a screen shot probably requires dynamic memory allocation though, and that's one of the things that you cannot do in a signal handler. So, what you will have to do, is have the daemon wait on a condition variable and have the signal handler set it and notify the waiter.
It would be much simpler to start the daemon after X11 starts, rather than at boot.
Upvotes: 2
Reputation: 4891
It's not just a matter of waiting until the X server starts. To successfully connect to it:
DISPLAY
environmental variable.All those conditions are fulfilled if you simply start the program as part of the graphical session, instead of starting it independently after boot.
There are several ways to achieve start of program inside the graphical session. One is by putting something.desktop
file into /etc/xdg/autostart
directory. You can check other files from there for syntax.
Upvotes: 4