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