Beeblebrox
Posts: 8
Joined: Sat Apr 07, 2012 3:11 pm

Quick2wire I2C with crontab

Tue Nov 12, 2013 2:30 pm

Hi,

I've got a python script the talks to my ADC just fine.

However if I add it to crontab it fails.

* * * * * /usr/bin/python3 /home/pi/t.py

If I take the I2C stuff out it works under cron fine.

Is there something obvious I'm missing

import quick2wire.i2c as i2c
adc_address1 = 0x68



with i2c.I2CMaster() as bus:
def changechannel(address, adcConfig):
bus.transaction(i2c.writing_bytes(address, adcConfig))

def getadcreading(address):
h, l ,s = bus.transaction(i2c.reading(address,3))[0]
while (s & 128):
h, l, s = bus.transaction(i2c.reading(address,3))[0]
# shift bits to product result
t = (h << 8) | l
# check if positive or negative number and invert if needed
#if (h > 128):
# t = ~(0x020000 - t)
return t

while True:
changechannel(adc_address1, 0x98)
print ("Channel 1: %01f" % getadcreading(adc_address1))



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

Re: Quick2wire I2C with crontab

Tue Nov 12, 2013 7:50 pm

When you run in cron it doesn't set-up the same environment variables as we get in a normal shell or in sudo.

That can have an effect on what you're able to implicitly import in python.

I ran a little program to display the environment in cron

Code: Select all

0: HOME=/home/pi
1: LANG=en_GB.UTF-8
2: LOGNAME=pi
3: PATH=/usr/bin:/bin
4: PWD=/home/pi
5: SHELL=/bin/sh
and the same program in a shell

Code: Select all

0: HOME=/home/pi
1: LANG=en_GB.UTF-8
2: LOGNAME=pi
3: LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:
4: MAIL=/var/mail/pi
5: OLDPWD=/home/pi
6: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
7: PWD=/home/pi/python
8: SHELL=/bin/bash
9: SHLVL=1
10: SSH_CLIENT=10.1.1.36 55352 22
11: SSH_CONNECTION=10.1.1.36 55352 10.1.1.7 22
12: SSH_TTY=/dev/pts/3
13: TERM=xterm
14: USER=pi
15: XDG_SESSION_COOKIE=a3be5d98a3468313bf90a5c450031884-1384285494.3568-935250789
16: _=/usr/bin/python
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

BikerJim
Posts: 6
Joined: Sun May 11, 2014 10:13 pm

Re: Quick2wire I2C with crontab

Sun May 11, 2014 10:34 pm

Hi,
Sorry to wake up an old thread, but this one came up as the most likely solution through a couple of days of trying to fix this.. but alas no solution. I have the exact same problem, and the answer above helps me understand why its happening, but I am pulling my hair out here to find a solution? i.e. How do you force Crontab to see the quick2wire modules?

I have tried adding the following to /home/.profile and ./bashrc, both leave me with ":/home/pi/python/quick2wire-python-api" when I echo $PYTHONPATH, (which seems to indicate that I didn't have any PYTHONPATH to append to..) but still no joy.

Code: Select all

export PYTHONPATH=$PYTHONPATH:$HOME/python/quick2wire-python-api
I have tried adding this to the script itself before the import i2c lines:

Code: Select all

import sys
sys.path.append('/home/pi/python/quick2wire-python-api')
I have tried adding a PATH= variable directly under the shebang, and I have tried appending the q2w path to the path in /etc/crontab.

I have seen that the docs suggest that one should install quick2wire to a virtual environment, which might be an option, but it took me hours to compile and install matplotlib, and this pi is intended for use for one thing and one thing only...its all working perfectly otherwise.. (i.e. I comment out the quick2wire stuff it works)

I hope someone can shed some light, I don't have much hair left!

Thanks,
Jim

BikerJim
Posts: 6
Joined: Sun May 11, 2014 10:13 pm

Re: Quick2wire I2C with crontab

Mon May 12, 2014 1:30 pm

From another place, I found a tip to run the script under /bin/sh (the shell which crontab uses) instead of BASH, to ape the cron environment... and.. the script runs fine by calling it from there.

I should also mention that by using tail -f /var/log/syslog I can see the script being called by cron, so I know its calling the script, its just not successfully running it, also I am running it from crontab -e (not sudo crontab -e) so it shouldn't be a permissions issue I think?

:?

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Quick2wire I2C with crontab

Mon May 12, 2014 1:35 pm

Please post your /bin/sh script
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

BikerJim
Posts: 6
Joined: Sun May 11, 2014 10:13 pm

Re: Quick2wire I2C with crontab

Mon May 12, 2014 2:37 pm

Credit where credit's due: http://stackoverflow.com/questions/1012 ... h-and-user
and : https://groups.google.com/forum/#!topic ... sLb5EH9zBU

So, I managed to work it out.

By forcing the cronjob to write the output to a file:

Code: Select all

*/5 * * * * /home/pi/python/collect_data.py >> /home/pi/python/collector.log 2>&1
I could see the traceback, the last line telling me that it couldn't find gpio-admin.

OSError: [Errno 2] No such file or directory: 'gpio-admin'

So, a quick check to see where gpio-admin lived:

Code: Select all

which gpio-admin
told me that it was in /usr/local/bin/gpio-admin.

Cron's default path didn't know about the /usr/local/bin, only the /bin/ and /usr/bin.

So, I did 'echo $PATH' at the command line, and pasted the result into crontab like so:
crontab -e

Code: Select all

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

*/10 * * * * /home/pi/python/collect_data.py
I hope that helps someone having a similar problem.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Quick2wire I2C with crontab

Mon May 26, 2014 1:21 pm

Personally I would have added the full pathname to the script instead of messing with the PATH.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

BikerJim
Posts: 6
Joined: Sun May 11, 2014 10:13 pm

Re: Quick2wire I2C with crontab

Wed May 28, 2014 4:39 pm

I could be wrong but I think I tried that, and for some reason it didn't work, I think it might have been that the script doesn't run as root...

Return to “Interfacing (DSI, CSI, I2C, etc.)”