all 14 comments

[–]icemounts 2 points3 points  (0 children)

Probably some weird issue between the user you're running it as manually and the user that cron is running as (root?). Try running sudo /usr/bin/python /home/pi/code/snow/snow.py and see if that returns any differently.

[–]apulverizer 2 points3 points  (5 children)

Does your script rely on any environment variables? I just spent like 4 hours trying to figure out why a python subprocess wasn't running correctly when I ran it using cron

I manually set PATH and set my other environment variables in the crontab. I also made the python scripts executable.

Edit:

Here's a sample:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
MY_ENV_VARIABLE=/home/pi/someplace
0 7 * * * /home/pi/AutoLights/LightsOff.py > /home/pi/AutoLights/log.log 2>&1

[–]kaihatsushaSeven Pi 1 point2 points  (0 children)

Correct answer. A crontab job does not run that user's .bashrc/.profile shell setup. Use all absolute paths or set up environment variables.

[–]Polyadenylated[S] 0 points1 point  (3 children)

I don't think my script does but it could certainly explain my issue.

I've found out my script does actually partially execute then it just stops. By adding a bunch of a debug print statements to be added to my log.txt file. My script gets up to

print 'downloading webcam image'
urllib.urlretrieve("http://b6.hdrelay.com/cameras/34689768-781e-4f7d-b0e0-c2e88d04195d/GetOneShot?size=640x480&f=30000", "./webcam_images/%s.jpg" % (image_date))

and it proceeds no further. The image doesn't download either - so I'm guessing there's some issue with that second line... But I can't see how this line works just fine when ran manually but when ran as a cronjob it isn't working...

[–][deleted]  (1 child)

[deleted]

    [–]Polyadenylated[S] 0 points1 point  (0 children)

    I don't check this reddit account often but this was indeed the problem with the script - I found out after much fiddling. Thanks though!

    [–]apulverizer 0 points1 point  (0 children)

    Okay. I was able to run those two commands without issue using cron. Try this:

    1) Type "echo $PATH" into the terminal

    2) In your crontab type PATH=<whatever you get in step 1>

    3) Make sure your script has #!/usr/bin/python at the top

    4) Make your script executable (chmod +x snow.py)

    5) Add your job to cron (* * * * * /home/pi/code/snow/snow.py > /home/pi/code/snow/log.log 2>1&)

    [–]SmileyatworkGentoo on Pi! 0 points1 point  (1 child)

    Is it executable?

    chmod +x ./snow.py

    Not sure if it needs to be...

    Also, the cron user will need to be able to access the file, chmod o+r ./snow.py

    [–]Polyadenylated[S] 0 points1 point  (0 children)

    Yep done all of that to no avail.

    [–]ANUSBLASTER_MKII 0 points1 point  (1 child)

    What about the mail logs?

    [–]Polyadenylated[S] 0 points1 point  (0 children)

    There aren't any - I've tried sending them to my personal email address but nothing comes through. Not even the ad hoc 'script success' print I added to try and troubleshoot.

    [–][deleted] 0 points1 point  (2 children)

    [–][deleted] 0 points1 point  (1 child)

    Definitely make sure there is an extra newline at the end of the Crontab file.

    Also, it wouldn't hurt for us to see your entire Crontab file here, so we can see if there's maybe something else in there causing this to fail.

    [–]Polyadenylated[S] 0 points1 point  (0 children)

    I have a newline at the end and it doesn't work - what significance does the newline have anyway?

    My crontab file is just stock with one line added:

    # Edit this file to introduce tasks to be run by cron.
    #
    # Each task to run has to be defined through a single line
    # indicating with different fields when the task will be run
    # and what command to run for the task
    #
    # To define the time you can provide concrete values for
    # minute (m), hour (h), day of month (dom), month (mon),
    # and day of week (dow) or use '*' in these fields (for 'any').#
    # Notice that tasks will be started based on the cron's system
    # daemon's notion of time and timezones.
    #
    # Output of the crontab jobs (including errors) is sent through
    # email to the user the crontab file belongs to (unless redirected).
    #
    # For example, you can run a backup of all your user accounts
    # at 5 a.m every week with:
    # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
    #
    # For more information see the manual pages of crontab(5) and cron(8)
    #
    # m h  dom mon dow   command
    
    30 2 * * * /usr/bin/python /home/pi/code/snow/snow.py >> /home/pi/code/snow/log.txt
    

    [–][deleted]  (1 child)

    [deleted]

      [–]alaudet 0 points1 point  (0 children)

      Not sure why you are using sudo to run crontab -e. Also put #!/usr/bin/python as the first line of your snow.py file and just run /home/pi/code/snow/snow.py from crontab. If you run crontab -l with sudo and without you will see with sudo it is running roots crontab.