Terminal Script to Take Screenshots in Ubuntu 24 and Send to Another Server

Here’s the Bash Script to Take a Screenshot Using ImageMagick

First, you need to make sure ImageMagick is installed

sudo apt update && sudo apt install ImageMagick
#!/bin/bash
# Script for sending a screenshot of an X11 compositor to a server
# Author: Mathew Moore | https://nwdigital.cloud

# mute the speakers
amixer set Master mute
# get the local display, must be using Xorg, not Wayland.
# to enable Xorg, sudo nano /etc/gdm3/custom.conf and uncomment #WaylandEnable=false
export DISPLAY=:0

# The server stuff
local_ip=$(hostname -I | awk '{print $1}')
screenshot="${HOME}/imgout.jpg"
host="https://core.smashingwordpress.com"
interval=15

# remove any existing screenshots before take a new one
rm $HOME/imgout*

# The loop
while true; do

# remove the previous screenshot if one exists
if [ -f "${screenshot}" ]; then
   rm ${screenshot}
fi

   # sudo apt update && sudo apt install ImageMagick
   import -window root "${screenshot}" 

   if [ -f "${screenshot}" ]; then
      curl -sS -k -F "ip=${local_ip}" \
      -F "hostname=${HOSTNAME}" \
      -F "file=@/${screenshot}" "$host/wp-json/nwd-ubcore/v1/register"
   else
      curl -sS -k -F "ip=${local_ip}" \
      -F "hostname=${HOSTNAME}" "$host/wp-json/nwd-ubcore/v1/register"
   fi
   sleep ${interval}
   
done

Here’s the Script to Launch Firefox in Kiosk Mode Using Screen

Create the firefox script

# Create the file
touch $HOME/Desktop/firefox.sh

# Make the file executable
chmod +x $HOME/Desktop/firefox.sh

# Open the file for editing
nano $HOME/Desktop/firefox.sh

Add the contents below to the firefox.sh file, then save and close it.

url="https://nwdigital.cloud"

export DISPLAY=:0

/snap/bin/firefox --CreateProfile "default"
/snap/bin/firefox -P default -turbo -purgecaches -private-window \
--kiosk --disable-pinch \
--kiosk "${url}"

System Service For The Kiosk

[Unit]
Description=Kiosk Service by Matt Moore
After=network.target network-online.target

[Service]
User=yourusername
Type=simple
RestartSec=3
ExecStart=/home/yourusername/Desktop/firefox.sh
ExecStop=/bin/bash -c 'pgrep firefox 1> /dev/null && pkill firefox'


[Install]
WantedBy=graphical.target

Turn the Screenshot Script into a Systemd Service

Create a new service fil

sudo nano /etc/systemd/system/screenshot.service

Add the following to the screenshot.service file and save it. Be sure to change ‘yourusername’ to the one you’re using and update any paths as necessary.

[Unit]
Description=Screenshot Service for Kiosk by Matt Moore
After=network.target network-online.target

[Service]
User=yourusername
Type=simple
Restart=always
RestartSec=3 
ExecStart=/home/yourusername/Desktop/take-screenshot.sh

[Install]
WantedBy=graphical.target

After saving the file, enable the service and start it.

sudo systemctl enable screenshot.service && sudo systemctl start screenshot.service

Add it to Your AutoStart Instead of a System Service

cd $HOME/.config/autostart

sudo nano take-screenshot.sh.desktop

Add the following contents to the take-screenshot.sh.desktop file you just opened above

[Desktop Entry]
Type=Application
Exec=/home/yourusername/Desktop/take-screenshot.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=Take Screenshot for Kiosk
Name=Take Screenshot for Kiosk
Comment[en_US]=
Comment=Takes a screenshot and sends it to my server

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.