You are not logged in.

#1 2018-07-30 04:03:25

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

[SOLVED] How to Programatically Capture Screenshot in a Linux Service

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

#2 2018-07-30 06:31:49

ayekat
Member
Registered: 2011-01-17
Posts: 1,590

Re: [SOLVED] How to Programatically Capture Screenshot in a Linux Service

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.

tony5429 wrote:

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?


pkgshackscfgblag

Offline

#3 2018-07-30 13:31:30

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How to Programatically Capture Screenshot in a Linux Service

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

#4 2018-07-30 13:49:58

ayekat
Member
Registered: 2011-01-17
Posts: 1,590

Re: [SOLVED] How to Programatically Capture Screenshot in a Linux Service

tony5429 wrote:

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.

See https://wiki.archlinux.org/index.php/Sy … XAUTHORITY


pkgshackscfgblag

Offline

#5 2018-07-30 14:04:07

N_BaH
Member
Registered: 2015-12-30
Posts: 84

Re: [SOLVED] How to Programatically Capture Screenshot in a Linux Service

hi,

what if X does not run? the service executes the script, but cannot find DISPLAY. hmm

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 ( smile ), from kind-of-autostart ?

Offline

#6 2018-07-31 14:29:08

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How to Programatically Capture Screenshot in a Linux Service

@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

#7 2018-07-31 14:34:56

ayekat
Member
Registered: 2011-01-17
Posts: 1,590

Re: [SOLVED] How to Programatically Capture Screenshot in a Linux Service

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?

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. neutral


pkgshackscfgblag

Offline

#8 2018-07-31 14:40:45

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,530
Website

Re: [SOLVED] How to Programatically Capture Screenshot in a Linux Service

ayekat wrote:
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. neutral

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

Online

Board footer

Powered by FluxBB