You are not logged in.
Pages: 1
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
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
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
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
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
Offline
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
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
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
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
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
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
Skanky, Can you explain a bit more as to what I need to do. I couldn't get exactly what you told.
Thanks
Offline
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
No. The behavior is still the same.
Offline
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
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
I tried it skanky. No change
Offline
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
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
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
If anyone know any other way of getting screenshot from python. Do suggest. I will try that.
Offline
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
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
Thanks Trent. Will try with scrot and post.
But with the
import -window root jjj.png
command knows what screenshot to take right?
Offline
Pages: 1