简体   繁体   中英

crontab automated python script won't upload to dropbox

I am running a Raspberry pi with connected camera and want to use crontab to automatically upload taken pictures once per hour.

Pictures are taken by another shell script every 6 minutes, which is also controlled by crontab and works fine.

The python script to upload the files works as intended when calling it from the shell. It builds the correct file names and uploads them. Here is the script:

#!/usr/bin/env python 
import dropbox
import os.path
import time
#pictures are taken every 6 minutes so this is used to build the file names:
interval = ('00','06','12','18','24','30','36','42','48','54')

access_token = "my_token"
client = dropbox.client.DropboxClient(access_token)

#get current time
t = time.strftime("%Y-%m-%d_%H")

for x in interval:
        #build file name:
        file =  t+x+'.jpg'
        #check if file exsists:
        if os.path.isfile(file):
                #open file
                pic = open(file, 'rb')
                #upload picture:
                response = client.put_file(file, pic)
                print "picture " + file + " uploaded"
                pic.close()

I added the following in crontab -e (the path is correct). So that it should upload every hour at :56 minutes

56 * * * * python /home/pi/Pictures/pyPictureUpload.py

However, nothing is ever uploaded. I also tried it with the same command in a shell script and call the shell script in crontab instead:

56 * * * * /home/pi/Pictures/runUpload.sh

This is the very short shell script:

!/bin/bash
python /home/pi/Pictures/pyPictureUpload.py

All scripts work as intended when called from the shell directly but nothing works automatized by crontab. What am I doing wrong?

There could be many reasons for the failure.

(this is an answer that I provided yesterday for a similar question but I do not know how to insert a link from my phone app. I may edit this tomorrow)

The environment variables of the cron daemon are not the same as the login shells.

In particular the PATH variable which is used to locate commands might not be the one you expect: some commands would not be found in such situation, for example.

Could you provide all errors and output generated when the script was run by cron daemon?


How to debug a script executed by the cron daemon?

At first, add set -x to the script in order to make bash generates trace messages:

$ cat /home/william/bin/script.sh
#!/bin/bash --
set -x

Solution 1

By default error output and standard output are all gathered and sent into an email to the current user mailbox. All logins have their own local mailbox on linux and UNIX. Connect as the user which own the crontab and type mail :

$ crontab -l
* * * * * /home/william/bin/script.sh
$ mail

The mail command offers a text interface and enables you to list and read emails received. There is a help.

Solution 2

Edit the crontab and redirect output to a file on the disk

$ crontab -l
* * * * * /home/william/bin/script.sh 2>/tmp/script.out >&2

Wait that cron daemon runs the script and debug with information logged into the /tmp/script.out file.

I suspect it's a problem with the working directory being different in the shell versus when cron is running. My experience in similar situations has been that it is safer to specify the full path every time. The os.path module has a number of helpful functions for this.

The problem being that you define the filename as file = t+x+'.jpg' but never give a directory where those files are stored. If you are manually running the python script in the same directory as the images, it will work fine. But when the cron daemon runs it, it isn't starting in that same directory.

Try:

...
basePath = '/path/to/my/images'
for x in interval:
    #build file name:
    file = os.path.join(basePath, t+x+'.jpg')
    #check if file exsists:
    if os.path.isfile(file):

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM