edwardattardmontalto
Posts: 14
Joined: Thu Aug 31, 2017 7:52 pm

CRON scheduler runs - but nothing happens

Wed Sep 13, 2017 4:10 pm

I am using a python script that reads the tempreature from a temprature prob and then spins a motor based on the tempreature. Once thats done, I log that the motor has spun into a log file. The pi is the model 2B and is NOT connected to wifi. Code below:

Code: Select all

# -*- coding: utf-8 -*-

import MotorControl

import RPi.GPIO as GPIO
import datetime
import os
import glob
import collections
import subprocess

##Sets up GPIO pins for motor
def establishMotorPins():
    ##Setting up pins and enabling the motor    
    MotorControl.setUpGPIOPins(27, 24, 5)
    GPIO.output(5, GPIO.HIGH)

##Spins the motor - feeding the fish
def feedFish(duration):
    ##print("Feedig time!")    
    MotorControl.motorCycle(27, 24, 'c', duration) #c standing for clockwise

##Write to a log file
def writeToFile(t, s):
    logFile.write("\nFish have been fed on: " + str(datetime.datetime.now()) +
                  " Tempreature is (°C): " + str(t) + " Speed: " + s)
    logFile.flush()
    os.fsync(logFile.fileno())

##Reading tempreature from sensor (tempreature is stored in file)
def read_temp_raw():
    catdata = subprocess.Popen(['cat',device_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out,err = catdata.communicate()
    out_decode = out.decode('utf-8')
    lines = out_decode.split('\n')
    return lines

##Extracting tempreature from reading stored in text file
def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string)/1000.0
        temp_f = temp_c*9.0/5.0 + 32.0
        return temp_c #include ,temp_f to return temp F

##Defining a function as my main method
def main():

    temp = read_temp()

    if(temp >= 0 and temp <= 5):
        feedFish(0.5)
        speed = "a"
    elif(temp > 5 and temp <= 10):
        feedFish(0.5)
        speed = "b"
    elif(temp > 10 and temp <= 15):
        feedFish(1)
        speed = "c"
    elif(temp > 15 and temp <= 20):
        feedFish(2)
        speed = "d"
    elif(temp > 20 and temp <= 25):
        feedFish(3)
        speed = "e"
    elif(temp > 25 and temp <= 30):
        feedFish(4)
        speed = "f"
    elif(temp > 30 and temp <= 35):
        feedFish(4.5)
        speed = "f"
    elif(temp > 35 and temp <= 40):
        feedFish(5)
        speed = "g"
    elif(temp > 40):
        feedFish(5)
        speed = "h"

    writeToFile(temp, speed)

##
##Calling the pre-defined functions
##

##Setting up connection to the tempreature sensor
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'


##Setting up a log file
logFile = open("/home/pi/Desktop/AutomaticFishFeeder/log.txt", "a") ##use w to write or a to append to file
os.fsync(logFile.fileno())

try:
    establishMotorPins()
    ##remember to change direction of spin!!!!! 
    main()        

    GPIO.output(5, GPIO.LOW)    
    quit()
except KeyboardInterrupt:
    print("keyboard interrupt detected, program will exit")
except SystemExit:
    print("SystemExit exception caught, program has terminated.")
finally:
    logFile.close()
    GPIO.cleanup()
I am using cron scheduler to run this script twice a day, once at 9am and once at 4pm. The script ran this morning at 9am but nothing happened at 4pm. I've checked the raspbery pis log and at 4pm there is an entry in the pi's log that look like the following: Sep 13 16:00:01 raspberrypi

Code: Select all

CRON[16792]: (pi) CMD (python ~/Desktop/AutomaticFishFeeder/CronSchedule.py # JOB_ID_3) 
The exact same line appears in the raspberry pis logs at 9am aswell, and the log file I use to register when the motor spins does not record anything at 4pm. I used the GUI helper from cron and both entrys say that the script will be run at 9am and 4pm each day.

Any ideas? Could the pi be faulty?

Silverlock
Posts: 47
Joined: Wed Apr 10, 2013 3:51 am

Re: CRON scheduler runs - but nothing happens

Wed Sep 13, 2017 4:20 pm

Is there a ~ in the command line in the crontab file? If so, try replacing it with /home/pi (or whichever user you're running this command under).
RPi3+external HDD/Raspbian/Pixel desktop

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

Re: CRON scheduler runs - but nothing happens

Wed Sep 13, 2017 9:03 pm

If your Cron job runs at 9 but not at 4 the problem might be in your Cron file

Post your Cron file so we can see what you have as entries for each time
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help

edwardattardmontalto
Posts: 14
Joined: Thu Aug 31, 2017 7:52 pm

Re: CRON scheduler runs - but nothing happens

Fri Sep 15, 2017 6:37 am

Silverlock wrote:
Wed Sep 13, 2017 4:20 pm
Is there a ~ in the command line in the crontab file? If so, try replacing it with /home/pi (or whichever user you're running this command under).
Yes there is, I've replaced it with /home/pi for both the path of the python script and the path to python itself. Thanks, hopefully it works now :D

edwardattardmontalto
Posts: 14
Joined: Thu Aug 31, 2017 7:52 pm

Re: CRON scheduler runs - but nothing happens

Fri Sep 15, 2017 6:40 am

pcmanbob wrote:
Wed Sep 13, 2017 9:03 pm
If your Cron job runs at 9 but not at 4 the problem might be in your Cron file

Post your Cron file so we can see what you have as entries for each time
Hi, contents of the cron file look like:

Code: Select all

* 9 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
* 16 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
So the only difference is that one is set for 9 and the other 16

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

Re: CRON scheduler runs - but nothing happens

Fri Sep 15, 2017 11:38 am

edwardattardmontalto wrote:
Fri Sep 15, 2017 6:40 am

Code: Select all

* 9 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
* 16 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
Well I would have thought you should have had a minute figure in the as well if you only wanted it to run once at 9am and once at 4pm

try running with just one cron line like this

Code: Select all

 0 9,16 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
or if you want to use 2 cron line then this

Code: Select all

 0 9 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
 0 16 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py  

I always find this site helpful when setting up cron times https://crontab-generator.org/
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help

SkyRise
Posts: 135
Joined: Tue Jan 24, 2012 1:20 pm

Re: CRON scheduler runs - but nothing happens

Fri Sep 15, 2017 12:57 pm

You might also need to put in

Code: Select all

/path/to/python

edwardattardmontalto
Posts: 14
Joined: Thu Aug 31, 2017 7:52 pm

Re: CRON scheduler runs - but nothing happens

Fri Sep 15, 2017 2:05 pm

pcmanbob wrote:
Fri Sep 15, 2017 11:38 am
edwardattardmontalto wrote:
Fri Sep 15, 2017 6:40 am

Code: Select all

* 9 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
* 16 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
Well I would have thought you should have had a minute figure in the as well if you only wanted it to run once at 9am and once at 4pm

try running with just one cron line like this

Code: Select all

 0 9,16 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
or if you want to use 2 cron line then this

Code: Select all

 0 9 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
 0 16 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py  

I always find this site helpful when setting up cron times https://crontab-generator.org/
Copied your code exactly, even included the full path to python on my machine, and the motor still hasn't spun. Any other ideas?

edwardattardmontalto
Posts: 14
Joined: Thu Aug 31, 2017 7:52 pm

Re: CRON scheduler runs - but nothing happens

Fri Sep 15, 2017 2:06 pm

SkyRise wrote:
Fri Sep 15, 2017 12:57 pm
You might also need to put in

Code: Select all

/path/to/python
Change the cron to do that now, still doesn't work

SkyRise
Posts: 135
Joined: Tue Jan 24, 2012 1:20 pm

Re: CRON scheduler runs - but nothing happens

Fri Sep 15, 2017 2:10 pm

Just to be clear, which crontab are you editing and how are you doing it?

edwardattardmontalto
Posts: 14
Joined: Thu Aug 31, 2017 7:52 pm

Re: CRON scheduler runs - but nothing happens

Fri Sep 15, 2017 2:13 pm

SkyRise wrote:
Fri Sep 15, 2017 2:10 pm
Just to be clear, which crontab are you editing and how are you doing it?
Do you mean which entry in the crontab? if so both of them have been updated to the root of python on the pi. Again today it worked at 9 and not at 16

SkyRise
Posts: 135
Joined: Tue Jan 24, 2012 1:20 pm

Re: CRON scheduler runs - but nothing happens

Fri Sep 15, 2017 2:24 pm

Is the one at 4pm the last thing in the file? Is there a newline at the end (i.e. is there a blank line following the 4pm one?)

I have seen some programs which require a newline to terminate the last line or they don't function correctly. It is a long-shot though

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

Re: CRON scheduler runs - but nothing happens

Fri Sep 15, 2017 2:27 pm

edwardattardmontalto wrote:
Fri Sep 15, 2017 2:05 pm
pcmanbob wrote:
Fri Sep 15, 2017 11:38 am
edwardattardmontalto wrote:
Fri Sep 15, 2017 6:40 am

Code: Select all

* 9 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
* 16 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
Well I would have thought you should have had a minute figure in the as well if you only wanted it to run once at 9am and once at 4pm

try running with just one cron line like this

Code: Select all

 0 9,16 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
or if you want to use 2 cron line then this

Code: Select all

 0 9 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
 0 16 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py  

I always find this site helpful when setting up cron times https://crontab-generator.org/
Copied your code exactly, even included the full path to python on my machine, and the motor still hasn't spun. Any other ideas?

Well seeing as you updated cron and posted back at 15:05 it wont do any thing until 4pm or 9am tomorrow because that is the time you asked for it to run at.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help

edwardattardmontalto
Posts: 14
Joined: Thu Aug 31, 2017 7:52 pm

Re: CRON scheduler runs - but nothing happens

Fri Sep 15, 2017 3:52 pm

pcmanbob wrote:
Fri Sep 15, 2017 2:27 pm
edwardattardmontalto wrote:
Fri Sep 15, 2017 2:05 pm
pcmanbob wrote:
Fri Sep 15, 2017 11:38 am


Well I would have thought you should have had a minute figure in the as well if you only wanted it to run once at 9am and once at 4pm

try running with just one cron line like this

Code: Select all

 0 9,16 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
or if you want to use 2 cron line then this

Code: Select all

 0 9 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py 
 0 16 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py  

I always find this site helpful when setting up cron times https://crontab-generator.org/
Copied your code exactly, even included the full path to python on my machine, and the motor still hasn't spun. Any other ideas?

Well seeing as you updated cron and posted back at 15:05 it wont do any thing until 4pm or 9am tomorrow because that is the time you asked for it to run at.
I posted back at 16:05 my time

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

Re: CRON scheduler runs - but nothing happens

Fri Sep 15, 2017 6:00 pm

OK .

So assuming your program actually works if you run it from the command line.

try setting your cron up like this and then leave you pi alone till after 4pm tomorrow
then post back the contents of /home/pi/log.txt

Code: Select all

0 9 * * * date >>/home/pi/log.txt
0 9 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py >>/home/pi/log.txt 2>&1
0 16 * * * date >>/home/pi/log.txt
0 16 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py >>/home/pi/log.txt 2>&1
what this will do is post the time and date to the log file then it will post any output from your program to the log file including any errors.

it might be a good idea to un-comment any print statements in your program like the one that says feeding time and may be add one that prints the duration and in the main loop to print temp and speed.

this then might give us some indication of what's happening.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help

edwardattardmontalto
Posts: 14
Joined: Thu Aug 31, 2017 7:52 pm

Re: CRON scheduler runs - but nothing happens

Sat Sep 16, 2017 7:57 am

pcmanbob wrote:
Fri Sep 15, 2017 6:00 pm
OK .

So assuming your program actually works if you run it from the command line.

try setting your cron up like this and then leave you pi alone till after 4pm tomorrow
then post back the contents of /home/pi/log.txt

Code: Select all

0 9 * * * date >>/home/pi/log.txt
0 9 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py >>/home/pi/log.txt 2>&1
0 16 * * * date >>/home/pi/log.txt
0 16 * * * python /home/pi/Desktop/AutomaticFishFeeder/CronSchedule.py >>/home/pi/log.txt 2>&1
what this will do is post the time and date to the log file then it will post any output from your program to the log file including any errors.

it might be a good idea to un-comment any print statements in your program like the one that says feeding time and may be add one that prints the duration and in the main loop to print temp and speed.

this then might give us some indication of what's happening.
Ok great thanks, script worked this morning at 9am. Here are the contents of the /home/pi/log.txt file:

Code: Select all

Sat 16 Sep 09:00:01 CEST 2017
sh: 1: modprobe: not found
sh: 1: modprobe: not found
SystemExit exception caught, program has terminated.
And here are the contents of my log.txt file, showing that the script ran properly at 9:00

Code: Select all

Fish have been fed on: 2017-09-16 09:00:06.036100 Tempreature is (°C): 24.875 S$
I'll post the contents of both files again at 16:00, thank you very much for your help :D

User avatar
rpdom
Posts: 11426
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK

Re: CRON scheduler runs - but nothing happens

Sat Sep 16, 2017 8:12 am

Regarding the "modprobe not found" errors, perhaps you should put the full path to the modprobe command in your script: /sbin/modprobe, although I'm not sure why you are using that. Can you post the contents of the script?
[edit]
Ah, you have already done that.

These lines

Code: Select all

##Setting up connection to the tempreature sensor
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
should not be needed. Get rid of them. I think you have been following some old instructions. All you need to get the temperature sensor working is one line in /boot/config.txt

Code: Select all

dtoverlay=w1-gpio

edwardattardmontalto
Posts: 14
Joined: Thu Aug 31, 2017 7:52 pm

Re: CRON scheduler runs - but nothing happens

Sat Sep 16, 2017 8:17 am

rpdom wrote:
Sat Sep 16, 2017 8:12 am
Regarding the "modprobe not found" errors, perhaps you should put the full path to the modprobe command in your script: /sbin/modprobe, although I'm not sure why you are using that. Can you post the contents of the script?
Ok sure, script is below. I am using modprobe in order to interface the temperature sensor. Although it managed to read the temperature anyway ... :?:

Code: Select all

# -*- coding: utf-8 -*-

import MotorControl

import RPi.GPIO as GPIO
import datetime
import os
import glob
import collections
import subprocess

##Sets up GPIO pins for motor
def establishMotorPins():
    ##Setting up pins and enabling the motor    
    MotorControl.setUpGPIOPins(27, 24, 5)
    GPIO.output(5, GPIO.HIGH)

##Spins the motor - feeding the fish
def feedFish(duration):
    MotorControl.motorCycle(27, 24, 'c', duration) #c standing for clockwise

##Write to a log file
def writeToFile(t, s):
    logFile.write("\nFish have been fed on: " + str(datetime.datetime.now()) +
                  " Tempreature is (°C): " + str(t) + " Speed: " + s)
    logFile.flush()
    os.fsync(logFile.fileno())

##Reading tempreature from sensor (tempreature is stored in file)
def read_temp_raw():
    catdata = subprocess.Popen(['cat',device_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out,err = catdata.communicate()
    out_decode = out.decode('utf-8')
    lines = out_decode.split('\n')
    return lines

##Extracting tempreature from reading stored in text file
def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string)/1000.0
        temp_f = temp_c*9.0/5.0 + 32.0
        return temp_c #include ,temp_f to return temp F
            
##Defining a function as my main method
def main():
    
    try:
        temp = read_temp()
    except:
        feedFish(3.5)
        speed="ERROR"      
        writeToFile(temp, speed)
        return;
            
    if(temp >= 0 and temp <= 5):
        feedFish(0.5)
        speed = "a"
    elif(temp > 5 and temp <= 10):
        feedFish(0.5)
        speed = "b"
    elif(temp > 10 and temp <= 15):
        feedFish(1)
        speed = "c"
    elif(temp > 15 and temp <= 20):
        feedFish(2)
        speed = "d"
    elif(temp > 20 and temp <= 25):
        feedFish(3)
        speed = "e"
    elif(temp > 25 and temp <= 30):
        feedFish(4)
        speed = "f"
    elif(temp > 30 and temp <= 35):
        feedFish(4.5)
        speed = "f"
    elif(temp > 35 and temp <= 40):
        feedFish(5)
        speed = "g"
    elif(temp > 40):
        feedFish(5)
        speed = "h"
                  
    writeToFile(temp, speed)

##
##Calling the pre-defined functions
##
    
##Setting up connection to the tempreature sensor
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'


##Setting up a log file
logFile = open("/home/pi/Desktop/AutomaticFishFeeder/log.txt", "a") ##use w to write or a to append to file
os.fsync(logFile.fileno())
    
try:
    establishMotorPins()
    ##remember to change direction of spin!!!!! 
    main()        
    
    GPIO.output(5, GPIO.LOW)    
    quit()
except KeyboardInterrupt:
    print("keyboard interrupt detected, program will exit")
except SystemExit:
    print("SystemExit exception caught, program has terminated.")
finally:
    logFile.close()
    GPIO.cleanup()


User avatar
DougieLawson
Posts: 29372
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: CRON scheduler runs - but nothing happens

Sat Sep 16, 2017 8:49 pm

If you add that one line (as noted by Dom here posting.php?mode=reply&f=28&t=193177#pr1211609) to /boot/config.txt you can completely remove any trace of running /usr/bin/sudo /sbin/modprobe from your python program.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

edwardattardmontalto
Posts: 14
Joined: Thu Aug 31, 2017 7:52 pm

Re: CRON scheduler runs - but nothing happens

Tue Sep 19, 2017 9:30 am

Ok so it turns out the issue was originating from the modprobe packages. Removed both of them and now everything works perfectly well! Thank you all very much for your advice :D 8-)

Martin Frezman
Posts: 970
Joined: Mon Oct 31, 2016 10:05 am

Re: CRON scheduler runs - but nothing happens

Tue Sep 19, 2017 11:57 am

Funny how that works...
If this post appears in the wrong forums category, my apologies.

Return to “Troubleshooting”

Who is online

Users browsing this forum: No registered users and 36 guests