How to Take and Get Screenshot from Gnome Desktop via SSH

fedoragnomescreenshotsshwayland

I set up a automated kickstart-installation for a "digital-signage-client" based on Fedora 30 (soon 32), and I need to get an actual visual feedback what is on the screen right now. I tried to enable the Gnome-Remote-Desktop via commandline (see Enable Gnome Screen Sharing via Commandline?) but unfortunately I'm not able to.

So the new approach is, to take a screenshot (and copy it via scp). While I'm able to take a screenshot directly on the client via a gnome-terminal with gnome-screenshot, I don't know how I can do this from remote. I also tried other tools like KDE Spectacle or Shutter but also without luck.

An Idea was to set a cronjob to take the screenshot:

$> crontab -e

1 * * * * gnome-screenshot

but this doesn't work eighter. journalctl _COMM=cron says -- No entries --

Has someone an idea how I can take a screenshot from a Gnome-Wayland-Fedora Desktop? Should this work via crontab?

Best Answer

For Wayland

Based on information from n-tchen, Flameshot (in the Fedora repos) works well:

flameshot screen -p ~/

If you are connected via ssh, add WAYLAND_DISPLAY=wayland-0 at the beginning of the above line.

You can also use gnome-screenshot instead of Flameshot, but you'll still need the above addition for ssh.

Additional information (source):

The cross-platform way to take screenshots on Wayland is via xdg-desktop-portal (which also works outside of Flatpak). See https://github.com/flatpak/xdg-desktop-portal/blob/master/data/org.freedesktop.portal.Screenshot.xml

Finally, Pyscreenshot supports Wayland; see the GitHub README

For X11

The import command from ImageMagick does a great job of screenshots:

import -silent -window root /tmp/screenshot.jpg

If you are connected via ssh, add DISPLAY=:0.0 at the beginning of the above line, and you'll need to be logged in as the same user that is logged in locally.

If you want to run this in crontab, here is how to set it up. This will take a screenshot every 6 minutes and store it with a date-time-encoded filename in /root/monitor/. It has been tested on Ubuntu 16.04 and 18.04.

echo -n "/bin/bash -c '"                                                                  >cronexec
echo -n   'export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin && ' >>cronexec
echo -n   'export HOME=/root && '                                                        >>cronexec
echo -n   'export D1=/tmp/gneemp && '                                                    >>cronexec
echo -n   'export F2=`date --utc +/root/monitor/%Y-%m-%dT%H:%M.jpg` && '                 >>cronexec
echo -n   'mkdir -p $D1 && '                                                             >>cronexec
echo -n   'chmod 643 $D1 && '                                                            >>cronexec
echo -n   'DISPLAY=:0.0 sudo -u `who --users |head -1 |grep -o "^\S*"` import -quiet -silent -window root $D1/tmp.jpg && '  >>cronexec
echo -n   'mkdir -p /root/monitor && '                                                   >>cronexec
echo -n   'mv $D1/tmp.jpg $F2 && '                                                       >>cronexec
echo -n   'rmdir $D1 '                                                                   >>cronexec
echo    "'"                                                                              >>cronexec
perl -p -i -e 's|\%|\\\%|g;' cronexec # from the manpage:  "Percent-signs (%) in the command, unless escaped with backslash (\) ..."
(sudo crontab -l 2>/dev/null; echo -n "*/6 * * * * "; cat cronexec) |sudo crontab - && rm cronexec
Related Question