helpaohe
Posts: 10
Joined: Wed Apr 01, 2020 9:18 am

CRON: Pyscreenshot all backends failed

Wed Jul 15, 2020 6:49 pm

Dear Community

I am trying to get a program from Vitor (https://stackoverflow.com/questions/578 ... attachment) to run every 15 minutes with crontab. The script works fine if I run it from the terminal with the following command

Code: Select all

sudo python /home/<username>/somepath/webscreen.py 
I then tried to get it to run with cron, but it simply didn't work. I made sure that cron works (and it does when using other scripts) and have used this troubleshooting guide (https://garyhall.org.uk/troubleshooting ... ry-pi.html). I got to point three and there I found the problem, however, I cannot solve it. Point three suggests to try if the "very limited" cron environment can run the job by running the following command:

Code: Select all

env -i /bin/bash --noprofile --norc
This command leads me to the bash-5.0$. If I then run the first mentioned command (running the script) in bash-5.0$, then I get the following error:

Code: Select all

bash-5.0$ sudo python /home/<username>/somepath/webscreen.py 
Traceback (most recent call last):
  File "/home/<username>/somepath/webscreen.py", line 44, in <module>
    main()
  File "/home/<username>/somepath/webscreen.py", line 39, in main
    imagem = ImageGrab.grab()
  File "/usr/local/lib/python2.7/dist-packages/pyscreenshot/__init__.py", line 31, in grab
    return backend_grab(backend, bbox, childprocess)
  File "/usr/local/lib/python2.7/dist-packages/pyscreenshot/loader.py", line 147, in backend_grab
    return auto(bbox, childprocess)
  File "/usr/local/lib/python2.7/dist-packages/pyscreenshot/loader.py", line 127, in auto
    raise FailedBackendError(msg)
pyscreenshot.err.FailedBackendError: All backends failed!
This is the python code which I am using (and which works if run from terminal with sudo priveliges):

Code: Select all

import smtplib
#from PIL import ImageGrab (does not work since only for mac / windows)
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import os.path
import pyscreenshot as ImageGrab

def enviar_email():
    email = 'Someemail'
    password = 'Somepassword'
    subject = 'Info'
    message = 'Created by Vitor, edited by Marco'
    file_location = 'Some.jpg'

    msg = MIMEMultipart()
    msg['From'] = email
    msg['To'] = 'Someotheremail'
    msg['Subject'] = subject

    msg.attach(MIMEText(message, 'plain'))

    # Setup the attachment
    filename = os.path.basename(file_location)
    attachment = open(file_location, "rb").read()
    image = MIMEImage(attachment, name=filename)
    msg.attach(image)

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(email, password)
    text = msg.as_string()
    server.sendmail(email, email, text)
    server.quit()

def main():
    imagem = ImageGrab.grab()
    imagem.save('Stats.jpg', 'jpeg')
    enviar_email()

if __name__ == '__main__':
    main()
Since all permissions are correct and the script runs from terminal, there is definitely something wrong with cron which needs to be adjusted. Any ideas are welcome.

Best regards

pcmanbob
Posts: 9462
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: CRON: Pyscreenshot all backends failed

Thu Jul 16, 2020 8:29 am

Any program run using cron is run in the background and no display/screen is linked to the running program , so I suspect that's why your program will not run from cron, there is not screen for pyscreenshot to take a shot of..
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

geektechstuff.com
Posts: 40
Joined: Sat Mar 02, 2019 8:08 pm
Contact: Website

Re: CRON: Pyscreenshot all backends failed

Fri Jul 17, 2020 4:25 pm

pcmanbob's answer is probably correct, but out of curiosity you are running the Python with sudo, are you using the regular users crontab or the sudo crontab (i.e. crontab -e or sudo crontab -e)?
www.geektechstuff.com

Return to “Python”