0

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?

1
  • Can you show us your script? How have you tried to debug this issue? Putting some print statements in your code might help. Where did you add the cron entry (@reboot log.sh)? If it's getting run as root, have you tried manually running your script as root (or are you doing that already)? Commented Dec 22, 2017 at 17:20

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.