Capturing Screenshot of terminal application via shell script?

I don't think you'll be able to do this with screen unless the output is actually rendered in a window, which probably defeats the point of using screen. However, the window does not have to be in the foreground.

The ImageMagick suite contains a utility called import you can use for this. If import --help gives you "command not found", install the imagemagick package, it will be available in any linux distro.

import needs the name of the window. iftop is a terminal interface, so to make sure you use the right name, you'll have to set the title of the GUI terminal it runs in. How you do that depends on which GUI terminal you use. For example, I prefer the XFCE Terminal, which would be:

Terminal -T Iftop -e iftop

Opens a new terminal running iftop with the title "Iftop". A screenshot of that can be taken:

import -window Iftop ss.jpg

If you are going to do this every five seconds, you probably want to instead open the window running a script so you can reuse the same terminal:

count=0;
while ((1)); do
    iftop &
    pid=$!
    sleep 1  # make sure iftop is up
    count=$(($count+1))
    import -window Iftop iftop_sshot$count.jpg
    kill $pid
    sleep 5
done

If the script is "iftopSShot.sh" then you'd start this Terminal -T Iftop -e iftopSShot.sh -- except you're probably not using Terminal. Most of the linux GUI terminals are associated with specific DE's, although they are stand-alone applications which can be used independently. I believe the name of the default terminal on KDE is Konsole and it follows the -T and -e conventions; for GNOME it is probably gnome-terminal (this may have changed) and it appears to use -t and not -T.

Beware import by default rings the bell, which will get irritating, but there is a -silent option.


In a X environment:


Set dynamically the terminal title:

From our script, a way to change the terminal title using ansi sequences:

echo -e "\033]0;Term | myApp\007";

Capture a png by window title:

Now we can search for the window id by the exact title using wmctrl and pass the id to the import utility:

import -window $(wmctrl -l | grep -i 'Term | myApp' | awk '{print $1}') ~/Pictures/capture.png

Build a gif:

Example to tweak, take 5 captures every second then convert them in order to a gif using convert, in an infinite 2 seconds loop.

rm -f /tmp/*png && for i in {1..5}; do import -window $(wmctrl -l | grep -i 'Term | myApp' | awk '{print $1}') /tmp/$i.png && sleep 1; done && convert -delay 200 -loop 0 /tmp/*.png animation.gif