Tuesday, June 2, 2015

take screen shots with terminal in Linux (bash script)

I recently needed to take screen shots in Linux with a tool automatically. I needed to have a 15 seconds time intervals between these screen captures and save them in a directory to do some image processing on them.

Since most linux distributaions use X Window system as their graphical system they are shipped with X utilities which can come handy here. xwd is a tool to  dump an image of an X window. It saves them in a special file format. but we can use pipes to convert it to for example png file format:

$ xwd | xwdtopnm | pnmtopng > /tmp/screenshot.png

We can have all of it as an bash script as well. name it myScript.sh for instance.

#!/bin/bash
# Produce a screen dump periodically and save as a JPEG
 

DELAY=15                   # seconds between screen dumps
DIR=/tmp/sceenshots  # directory to hold screen dumps
I=0                              # current image number
 

mkdir -p ${DIR}
while sleep $DELAY
 

do
xwd -root | xwdtopnm | pnmtopng >${DIR}/screendump.${I}.png
((I++))

done

just do

$chmod +x myScript.sh

and you are fine!

Tuesday, April 28, 2015

produce 100% CPU Load in Linux

Today, I was going to boot my old Thinkpad T61 with a live USB drive to test my CPU fan noise and performance. I was also using "Psensor", a tiny GTK+ based application to monitor the temperature of my CPU cores and fan speed. I wanted to have a 100% load for this matter.
I first thought of writing a CPU hungry program like Factorial calculation as recursive functions in C and run it on my Ubuntu Linux. One of my friends at work suggested a nice hack!

if you just enter this in Terminal:

dd bs=1024 if=/dev/urandom of=/dev/null

and then check resources usage through "htop" utility. you will be amazed! one of my CPU cores is running on 100%!







/dev/urandom is kernel's random number generator and what we do is to copy 512 Bytes into /dev/null! as simple as that! so CPU is busy creating random numbers!

We can run it again to have both cores run on 100%!
To give CPU a relief, we can send SIGTERM by pressing Ctrl+C to terminate the task. here is a an output after running for 10 minutes on my system:

4723473+0 records in
4723472+0 records out
4836835328 bytes (4.8 GB) copied, 426.123 s, 11.4 MB/s

Tuesday, April 7, 2015

bash completion in Ubuntu with root login

If you use sudo in bash to access root privilege when you are required to modify some system settings for example and use TAB key to have bash completion, you are fine.
But sometimes you want to completely switch to superuser you may invoke
su  command. In this case you are not able to use very handy bash TAB completion. To resolve this you should change .bashrc for the root user. Remember that with sudo you are still logged in with your current user not the root. so there is no need to alter bashrc for that user.

To enable it, you have to uncomment the lines in the picture to enable this.