You are not logged in.
I'm looking for a way to take a screenshot in a Linux service. This ImageMagick command works fine if I run it explicitly:
import -window root /home/bob/pic.png
But if I run it as a command called from a Linux service, I get this error:
import: unable to open X server `' @ error/import.c/ImportImageCommand/359.
I also tried prepending "export DISPLAY=:0.0" but that didn't help...
Last edited by tony5429 (2018-07-31 14:29:23)
Offline
What is a "Linux service"? Do you mean a systemd service? If so, are you running the command from a system-wide service? If so, consider using a user service instead.
I also tried prepending "export DISPLAY=:0.0" but that didn't help...
Where have you prepended that? Have you verified that the DISPLAY value for your session really is :0.0 (and not just :0)?
Also, what are really trying to do?
Offline
Ah, yes I meant a systemd service - thanks. Unfortunately it does need to be a system-wide service.
I had prepended it like this:
export DISPLAY=:0.0 ; import -window root /home/bob/pic.png
Ah, you're right; the DISPLAY value for my session is actually :0, but I just tried that and it also didn't work...
No protocol specified
import: unable to open X server `:0' @ error/import.c/ImportImageCommand/359.
I think from reading this page that I need to do something with /home/bob/.xprofile and /home/bob/.Xauthority, but I'm not sure what exactly. This is for a project writing an open-source alternative to the software at accountable2you.com.
Offline
Unfortunately it does need to be a system-wide service.
Why? The X session runs under your user, so why shouldn't the screenshot-capturing service run as your user? This seems needlessly complicated...
I had prepended it like this:
export DISPLAY=:0.0 ; import -window root /home/bob/pic.png
Yes, but where? If you put that line for ExecStart, this won't work, because systemd doesn't interpret the line like a shell (you'd need to wrap it into a sh -c '...' wrapper). But more generally, check the COMMAND LINES section in systemd.service(5) for how to set environment variables in service files.
I think from reading this page that I need to do something with /home/bob/.xprofile and /home/bob/.Xauthority, but I'm not sure what exactly.
Offline
hi,
what if X does not run? the service executes the script, but cannot find DISPLAY.
why don't you simply run the capture, inside an infinite loop checking for X to be running (if script doesn't stop when desktop stops ( ), from kind-of-autostart ?
Offline
@ayekat: OK, I read more about user services in the link you provided and realised that I'd been mistaken and that that really is a good solution for this effort. Switched it to a user service and that solved the problem. Thanks for your patience with me! Though irrelevant now, thanks for the other tips/info too. I'm using C and system() to wrap my shell commands.
Offline
I think it's also worth checking out the suggestion made by N_BaH. If you launch the X session in some way (e.g. with xinit), you could also just start your screenshot program in there, no?
I'm using C and system() to wrap my shell commands.
I... I don't think I want to know what exactly you're doing there.
Offline
tony5429 wrote:I'm using C and system() to wrap my shell commands.
I... I don't think I want to know what exactly you're doing there.
It's like seeing someone who was grotesquely mutilated in some industrial accident. You know you're not supposed to stare, but you just can't help it. You know you shouldn't ask about it, but you just feel you need to know.
In any case, it's disturbing.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline