up vote 4 down vote favorite
2
share [g+] share [fb]

To analyze my own computer usage, not to spy on anyone (although it crossed my mind), I want to have cron capture the current screen every minute.

 * * * * * /bin/bash -c "/usr/sbin/screencapture /somedir/screen.png"

in the crontab will execute and take a screen capture. However, it is totally black because it is not running as, well, me. Any idea how to allow a cron job to capture my screen?

Update: I added a say whoami to the same cron command and it confirms it is running as my user (no sudo or other users involved). I access the crontab from a terminal as myself.

So, it runs as me, but is not attached to my windowing system. Any ideas?

link|improve this question

Are you adding this to your system (root) crontab, or your user crontab? – Richard Hoskins Aug 21 '09 at 4:49
feedback

4 Answers

up vote 4 down vote accepted

If you look at the end of the screencapture manpage, you see it says:

To capture screen content while logged in via ssh, you must launch
screencapture in the same mach bootstrap hierarchy as loginwindow:

PID=pid of loginwindow
sudo launchctl bsexec $PID screencapture [options]

So I think you could do somethig like this in your shell script that cron calls:

#/bin/sh
loginwindowpid=`ps auwwx | grep loginwindow |grep -v grep  | cut -c12-15`
sudo launchctl bsexec $loginwindowpid screencapture /somedir/screen.png

Of course, you'll need your userid set to not need a password for sudo.
That is you'd set in /etc/sudoers with the visudo command

youruserid     ALL=(ALL) NOPASSWD: ALL
link|improve this answer
Wonderful solution to my question. Very complete. I had read the man page but same mach bootstrap hierarchy went right over my head so I headed straight to SU to help make sense of it. Thank you very much. – vgm64 Aug 22 '09 at 21:03
You can eliminate the call to grep -v by making the first call look like grep [l]oginwindow and might be able to eliminate the call to cut by using ps axo comm – Dennis Williamson Aug 25 '09 at 18:55
Very nice solution. – Laurent Etiemble Dec 15 '09 at 13:39
feedback

why not run the cron job as yourself?

link|improve this answer
feedback

If you are going to use sudo to set up cron for another account, you can use the -u switch along with sudo.

example:

sudo -u Your_user crontab -e

else just log into that account and use crontab -e.

link|improve this answer
feedback

As an aside - I use java for the same job - it starts on boot and I look thought the images at the end of the day...

    class ScreenCapture {
        public static void main(String args[]) throws
                AWTException, IOException {
                        // capture the whole screen
int i=1000;
                        while(true){
i++;
                                BufferedImage screencapture = new Robot().createScreenCapture(                                                new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())
 );

                                // Save as JPEG
                                File file = new File("screencapture"+i+".jpg");
                                ImageIO.write(screencapture, "jpg", file);

                                // Save as PNG
                                // File file = new File("screencapture.png");
                                // ImageIO.write(screencapture, "png", file);
try{
Thread.sleep(60*4*1000);
}
catch(Exception e){
e.printStackTrace();
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.