How to take Screenshots of Virtual Machine in VirtualBox under Linux

-By Vaibhav Kaushal

VirtualBox Virtual Machine Screenshot​VirtualBox might be famous for a million reasons. One thing it is certainly not famous for is the lack of the screenshot feature. It does not allow you to take the screenshot of the running virtual machine from the VM Window. This is one place where VMWare Workstation wins over VirtualBox! Or well, may be not! The fact is that VirtualBox does actually allow you to take screenshots of running VMs...from the mighty command line! And hey, I don't know about Windows or other furniture-type Operating Systems; I am talking about the Linux command line :D. Let us roll over to the command

Command to take Screenshots in VirtualBox

Take out the finest sword in the Linux arsenal (the Linux console) and write onto it the command (and read the lines below before pressing enter):

VBoxManage controlvm Mint screenshotpng ~/Desktop/Mint_Scr/LinuxMint.png;

First thing - this command will not work if you do it without understanding! The parts Mint and ~/Desktop/Mint_Scr/LinuxMint.png are actually the name of the guest OS window and the path where you want to save the file in respective order! Change them according to your need.

After you have done the above, the first thing that corsses the mind is "Wow, its wonderful." Second thing that crosses is "How the hell am I going to type that long command everytime I need a sceenshot?" The third thing that crosses is: "Okay, I will keep a terminal window open and will switch to the window when I need to take a screenshot and press the up arrow button and do it". The fourth is: "Oh! I have to edit the name of the file everytime I want to create a screenshot?"

Wait. There are more problems but before I or you mess up your or my brain, let us figure out the solution to the problem until now: What we need to modify that command to is this:

VBoxManage controlvm Mint screenshotpng ~/Desktop/Mint_Scr/img-`date +%s`.png;

The part `date +%s` will get replaced by a number which keeps changing every second automatically (the number is the UNIX Timestamp). Now, to take the first screenshot, type that command in the terminal and press enter. To take subsequent screenshots, siwtch to terminal, press the 'up arrow' key and press enter!

The need of automation

If you work with Virtual Machines, you know and I know this - taking screenshots is real cumbersome unless there is a keyboard shortcut! To first take the mouse pointer out of the window, then switch to the terminal window, press up button then press enter and then go back to the VM window - that is really cumbersome unless required only a couple of times; we need a keyboard shortcut! Now there is no simple way to do that. There might be some way to trick KDE to get that done (like create a shortcut to a plasma widget which launches a shell file containing that command) but that would still be difficult compared to an automated way to keep taking screenshots after regular intervals:

1. Create a new shell file and name it "takeshot.sh". 2. Put the following contents into the file and save it.

for (( ; ; ))
do
	VBoxManage controlvm Mint screenshotpng ~/Desktop/Mint_Scr/img-`date +%s`.png;
	read -t60 -p "Taking new screenshot in 60 seconds";
	echo "";
done

Now, launch the virtual machine and execute that shell file on the command line. This will produce a new file in the given directory in chronological filename order (as that is based on the seconds count) every 60 seconds and when it takes a picture, it will say that it is going to take another picture in 60 seconds. When you want to stop it, press Ctrl + C to stop it! But why 60 seconds? You will understand that when I explain the contents of the shell file one by one.

What did you put in the file

The shell file contains the follwing parts of code:

for (( ; ; )): This starts an infinite loop which will go on until you have pressed the Ctrl + C button on the terminal!

VBoxManage controlvm Mint screenshotpng ~/Desktop/Mint_Scr/img-`date +%s`.png: Command to take a new snapshot!

read -t60 -p "Taking new screenshot in 60 seconds": This asks the command line interpreter (bash shell) to print "Taking new screenshot in 60 seconds" on the terminal and wait (-p argument) for 60 seconds (-t60 argument). If you want to change the time interval, modify the "-t60" parameter and put the desired number of seconds in place of "60". If you want to modify the message, you know the way!

echo "": This just creates a new line to make it look pretty!

There are many ways you can extend the functionality of this program! I would just leave the possibilities to you. Please do tell me if you are stuck along and whether you liked this article! :)

Posted on Mar 29, 2013 12:06 AM
comments powered by Disqus