You are not logged in.

#1 2010-07-30 13:55:28

pramod1561
Member
Registered: 2010-07-21
Posts: 42

[solved] Unexpected Behavior

I am trying to get the screenshot every minute. I use "scrot" to get the screenshot. The code is like this :

#!/usr/bin/python

import sys, os, subprocess
from time import strftime

cwd = sys.path[0]
sfold = cwd + "/screenshot/"
prefix = strftime("%Y-%m-%d_%H:%M:%S")
filename = prefix + ".png"
sfile = sfold + filename
if not os.access(sfold, os.F_OK):
    # Creates the status directory if it doesn't exist
    os.mkdir(cwd + "/screenshot/", 0777)
# Get the screenshot and save it in the folder
scrtcmd = "scrot -q 30 " + sfile
subprocess.Popen(scrtcmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

When I run this from terminal it works fine. However when I add a cronjob like this,

* * * * * /path/to/the/above/file

The image is not getting dumped in the folder(The folder is created)

Any hunch on what the problem could be?

Last edited by pramod1561 (2010-08-13 19:30:03)

Offline

#2 2010-07-30 14:02:34

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [solved] Unexpected Behavior

I'm not speaking python, but I'd simply cd $folder && $take_screenshot ;P

There's even an example in the man page:
EXAMPLE
       scrot '%Y-%m-%d_$wx$h.png' -e 'mv $f ~/shots/'
       This would create a file called something like 2000-10-30_2560x1024.png and move it to your shots directory.

Last edited by karol (2010-07-30 14:12:31)

Offline

#3 2010-07-30 14:29:02

pramod1561
Member
Registered: 2010-07-21
Posts: 42

Re: [solved] Unexpected Behavior

Karol,

Thanks for the reply. This is only a part of the code. Entire Code is in Python. I would prefer solving it from python itself.

Offline

#4 2010-07-30 14:45:41

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [solved] Unexpected Behavior

Where is scrot located?
Cron can have a restricted PATH set, so you may need to set an expanded one at the top of your script.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#5 2010-07-30 15:00:43

pramod1561
Member
Registered: 2010-07-21
Posts: 42

Re: [solved] Unexpected Behavior

Karol, I tried to do that in a shell and call that shell from my python script. It dint work. Again the folder was created but not the image.

Skanky, Did you mean instead of the cwd use the full path? I tried that too and again the same behaviour sad

Offline

#6 2010-07-30 15:06:51

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [solved] Unexpected Behavior

I've use 'watch' instead of cron:
watch -n 5 -t ./sshot

#! /bin/bash
# sshot

scrot '%Y-%m-%d_%H.%M.%S_$wx$h.png' -e 'mv $f ~/shots/'

I got tons of pictures this way.

I really have no idea that's wrong with your python script.
Are the images created but in the wrong directory or they're not created at all?

Offline

#7 2010-07-30 15:14:04

pramod1561
Member
Registered: 2010-07-21
Posts: 42

Re: [solved] Unexpected Behavior

images are not created at all. As I told, If the script is run from terminal. Its working. So this has to do with something related to crontab.

Offline

#8 2010-07-30 15:23:49

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [solved] Unexpected Behavior

pramod1561 wrote:

images are not created at all. As I told, If the script is run from terminal. Its working. So this has to do with something related to crontab.

Ah, sorry, I'm a little slow today it seems.
Is the cron running?
If you can run the script manually, I guess it's executable.

Offline

#9 2010-07-30 15:26:48

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [solved] Unexpected Behavior

pramod1561 wrote:

Karol, I tried to do that in a shell and call that shell from my python script. It dint work. Again the folder was created but not the image.

Skanky, Did you mean instead of the cwd use the full path? I tried that too and again the same behaviour sad

I mean the path to scrot. IIRC (I should search first I guess) but cron is run with a path of:   /usr/bin:/bin

I've just installed it on this machine to check the man page but couldn't see a mention of it.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#10 2010-07-30 15:27:02

pramod1561
Member
Registered: 2010-07-21
Posts: 42

Re: [solved] Unexpected Behavior

Yeah the cron is running. When it is run from the crontab the folder screenshot (ref:original code) is created. But the image is not created. So, I can boil down the problem to these two lines of code :

# Get the screenshot and save it in the folder
scrtcmd = "scrot -q 30 " + sfile
subprocess.Popen(scrtcmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Offline

#11 2010-07-30 15:47:23

pramod1561
Member
Registered: 2010-07-21
Posts: 42

Re: [solved] Unexpected Behavior

Skanky, Can you explain a bit more as to what I need to do. I couldn't get exactly what you told.

Thanks

Offline

#12 2010-07-30 15:49:26

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [solved] Unexpected Behavior

pramod1561 wrote:

Skanky, Can you explain a bit more as to what I need to do. I couldn't get exactly what you told.

Thanks

His suggestion was to check if your script will run if you put it in /bin or /usr/bin.

Offline

#13 2010-07-30 16:01:15

pramod1561
Member
Registered: 2010-07-21
Posts: 42

Re: [solved] Unexpected Behavior

No. The behavior is still the same.

Offline

#14 2010-07-30 16:03:18

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [solved] Unexpected Behavior

Right, I've just installed scrot and it is in /usr/bin. If cron does indeed only run applications in a shell with a PATH=/usr/bin:/bin then it should find it. However, you could try this:

# Get the screenshot and save it in the folder
scrtcmd = "/usr/bin/scrot -q 30 " + sfile
subprocess.Popen(scrtcmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Which is sort of what I meant above (though I meant setting a new value to the PATH variable).


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#15 2010-07-30 16:03:58

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [solved] Unexpected Behavior

karol wrote:
pramod1561 wrote:

Skanky, Can you explain a bit more as to what I need to do. I couldn't get exactly what you told.

Thanks

His suggestion was to check if your script will run if you put it in /bin or /usr/bin.

No, the script can be found. It was scrot that I was questioning.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#16 2010-07-30 16:16:24

pramod1561
Member
Registered: 2010-07-21
Posts: 42

Re: [solved] Unexpected Behavior

I tried it skanky. No change sad

Offline

#17 2010-07-30 16:29:23

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [solved] Unexpected Behavior

You might want to search around and see what other restrictions cron's putting on its execution environment. The PATH one is the one that's got me before, but I'm sure there are others.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#18 2010-07-30 16:46:43

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,094

Re: [solved] Unexpected Behavior

Cron runs the stuff as root iirc, wich probably causes problems for scrot. Try wrapping it in su.

Last edited by Mr.Elendig (2010-07-30 16:47:55)


Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest

Offline

#19 2010-07-30 17:18:19

pramod1561
Member
Registered: 2010-07-21
Posts: 42

Re: [solved] Unexpected Behavior

Mr.Elendig,

I tried your suggestion. That doesn't create the folder too.

I add the cron job as a user. So, I think the cron would be run as the user only.

Offline

#20 2010-07-30 17:22:43

pramod1561
Member
Registered: 2010-07-21
Posts: 42

Re: [solved] Unexpected Behavior

If anyone know any other way of getting screenshot from python. Do suggest. I will try that.

Offline

#21 2010-07-30 17:56:29

pramod1561
Member
Registered: 2010-07-21
Posts: 42

Re: [solved] Unexpected Behavior

Hi all,

I installed image magick and tried this http://tips.webdesign10.com/how-to-take … untu-linux . Even then the behavior is same. When it is run freely it runs fine. But cronjob fails to dump the image.

Offline

#22 2010-07-31 04:23:18

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [solved] Unexpected Behavior

What is the value of the DISPLAY environment variable when you run it in a shell?

What is it when you run it from cron?

In effect, how does scrot know what to take a screenshot of?

Offline

#23 2010-07-31 14:31:19

pramod1561
Member
Registered: 2010-07-21
Posts: 42

Re: [solved] Unexpected Behavior

Thanks Trent. Will try with scrot and post.

But with the

import -window root jjj.png

command knows what screenshot to take right?

Offline

Board footer

Powered by FluxBB