Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use Cheese as my webcam software. I am trying to figure out a way to have it :

  • Start
  • Click a pic
  • Exit

when a script is executed. The script should not be asking for permissions and there shouldn't be any interruptions. So far,


I could only get it to do step 1. How do I do steps 2 and 3? The doc files don't mention an such option and I don't want to change the source code. (I don't mind camorama either)

share|improve this question
Have you seen this SO question or this one? – jasonwryan Jul 7 '12 at 1:45
I hadn't. I believed this SE is the only one which would have relevant questions. Anyways, the mplayer solution doesn't work for me. It outputs a blank (green) png. – user14517 Jul 7 '12 at 6:58
askubuntu.com/questions/253189/… Follow the link and you might get the answer you want. In the first three steps. after that you can write a crontab job. – user150853 Jan 10 at 6:31

Voilá! Steps 2 & 3 follow:

This works, although it's awfully timing critical, tweak as you see fit, tried to comment it decently so you can see what's going on.

You'll need to install xdotool for this to work, as we're simulating keypresses to take the picture and exit (package 'xdotool')

Oh, and you'll need to turn off the 'countdown' feature in preferences, otherwise it'll likely CTRL-Q (quit) out of the program before it actually takes the shot.

# L Nix <lornix@lornix.com>
# takeapic : take a photo with Cheese, using default settings, then exit
# start cheesing (2> because mine whines about cheesy stuff (ha!))
cheese 2>/dev/null &
# give WM some time to start up program (fails without this)
sleep 5
# set so we can determine if valid window(s) exist(s)
# wait for up to 90 seconds (tweak this)
while [ ${COUNTDOWN} -gt 0 ]; do
    WINDOWIDS=$(xdotool search --class "cheese" 2>/dev/null)
    if [ -n "${WINDOWIDS}" ]; then
    sleep 1
    COUNTDOWN=$(( ${COUNTDOWN} - 1 ))
# did we get anything?
if [ -z "${WINDOWIDS}" ]; then
    echo "Cheese never started, something's wrong"
    exit 1
# the shutter button is ALT-T
for WIDS in ${WINDOWIDS}; do
    # if you combine these like xdotool allows, it fails
    xdotool windowfocus ${WIDS} 2>/dev/null
    xdotool key alt+t 2>/dev/null
# pause a moment while taking photo
sleep 1
# now CTRL-Q out of the application
for WIDS in ${WINDOWIDS}; do
    xdotool windowfocus ${WIDS} 2>/dev/null
    xdotool key ctrl+q 2>/dev/null
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.