Katie Woolls
Katie Woolls

Reputation: 1

Raspberry pi crontab and interrupts in python

I wrote a python script that logs some data to a txt file with the absolute path /home/pi/foo.txt through button presses that trigger interrupts. I've started the script many times through the command line without any problems: when you press go, it goes, and when you press stop, it stops. The script is located in /home/pi/log.py. I wrote a shell script that will execute this python script because I read that it may help on a tutorial, so let's call it log.sh which calls

#! /bin/sh
cd /home/pi
/usr/bin/python /home/pi/log.py

However, when I attempt to start this script through crontab by adding it to my

@reboot log.sh

the script will run, but no button presses will stop the script (aka stop button won't work). The cpu usage goes up to 100% and sticks there. I've tried copying and pasting the environment variables from my user environment into crontab, but that won't work either.

Any ideas?

Upvotes: 0

Views: 335

Answers (1)

Michael Ababio
Michael Ababio

Reputation: 117

I learned the hard way that the environment that crontab uses isn't the same as your user's environment.

Check out this stack over flow for more info about environment difference between user and crontab: https://serverfault.com/questions/698577/why-is-the-cron-env-different-from-the-users-env/698635

The best thing in my opinion to do is to just do everything in python. There's a python environment that behaves like crontab. It's called schedule(https://pypi.python.org/pypi/schedule).

I'm actually using it in a raspberry Pi thermostat project. check it out here:https://github.com/mababio/raspi_thermostat/blob/c6aea6ded6874d0dc21ded34f07874dd7f97dd15/src/thermo/test/jobs.py

Upvotes: 1

Related Questions