Taking a Screenshot From A Daemon/Shell Script/Command Line

Taking a screenshot is very simple in Linux. Just hit the print screen button and we’re presented with a PNG file.But there are situations when we want to take a screenshot from a command line. Perhaps in a shell script, a daemon tool that we use to
monitor the user’s activity. We would like to take a screenshot without  the user having to press the print screen button to log a user’s  activity. Perhaps to record the on screen activity in response to some suspicious user action.

This is done using the xwd command in combination with other commands.

The following command can be used to take screenshot without the user
having to click on any window:

xwd -root | xwdtopnm | pnmtopng > Screenshot.png

For this command to work you need the following packages:

1. x11-apps
2. netpbm

Raja Sekharan is a freelance web developer who writes web development tutorials on his blog. Prototype Pro.

No Responses to “Taking a Screenshot From A Daemon/Shell Script/Command Line”

  1. Anthoniraj says:

    The above command is not producing proper png image in ubuntu , You can use the following two methods to take ScreenShot in Ubuntu

    1. Using xwd
    # xwd -root > screen.xwd
    # convert screen.xwd screen.png

    2.Using ImageMagic
    # import -window root screen.png

    Sample Shell Script for taking screen shot at regular interval for 24 hours( Every 5 Minutes)
    #vim capture.sh
    for((i=0;i<24;i++))
    do
    import -window root screen_$i.png
    sleep 5m
    done

  2. Anthoniraj says:

    The Above shell Script is used for taking screen shot at every 5 minutes [24 times]

  3. Great post! Just wanted to let you know you have a new subscriber- me!

Leave a Response