0

I have a script /home/projects/temperature_logger/analyze.py that does some work on a temp.log file, which is really a csv of a date and the current raspberry pi temperature. This python script also logs the results to a .log file. Calling it from the command line like sudo python3 analyze.py works as expected, but for some reason I cannot get it ot work with crontab.

I have run sudo chmod +x analyze.py as well as sudo chmod ugo+w analyze.py.

My whereis python3 says /usr/bin/python3. The shebang at the top of my analyze.py script is #!/usr/bin/python3.

My crontab script is * * * * * /usr/bin/python3 /home/projects/temperature_logger/analyze.py

For testing purposes all I have actually running right now is

if __name__ == '__main__':
    logging.info("running file!")

in my analyze.py but I do not see this getting populated to my log file unless I manually call sudo python3 analyze.py from within the directory.

What am I doing wrong here? The syslogs don't seem to indicate anything wrong but perhaps I don't understand them? Apologies for the picture I am unsure how to copy from the PuTTy terminal enter image description here

Update: I added all permissions for my user ubuntu with sudo chmod -R a+rwx /home/projects/temperature_logger. I then changed the crontab, cleared out my root cron and put the call to the python script under ubuntu's cron file. I do see this reflected under the syslog now enter image description here

It does not seem to indicate any error. However, the script which when I call it with python3 analyze.py it does write properly to my scripts.log file, but when it's called from the cron, which it says is running the syslog - I don't see anything logged.

Solved: So this was due to me misunderstanding waht the home directory for my user account was and what the current working directory of the script was. In my analyze.py script I had logging.basicConfig(filename='scripts.log', ...), which when I ran from the directory with sudo, wrote to /home/projects/temperature_logger/scripts.log, but when it was called from my user's cron file, it wrote to /home/ubuntu/scripts.log.

5
  • 1
    From PuTTy you can select and that goes into the clipboard. Then you just use Paste (Ctrl-V). – Eduardo Trápani Sep 28 '20 at 15:49
  • 1
    What user's crontab are you using. If the script requires root permissions to run, then use root's crontab. – Kusalananda Sep 28 '20 at 15:50
  • Does the script really require root (full system) privileges just to process a CSV file? – roaima Sep 28 '20 at 16:23
  • Let me ask a different way, since I'm going to be flashing ubuntu 20.04 to my raspberry pis memory card shortly anyways and then pulling /home/projects/temperature_logger from my git repo. Where would I want my project file? I made up the directory /projects/ under home to start working on coding, but I don't know what the convention is and if thats affecting permissions at all. And then, from who's cron would I want to be calling the analyze.py? – bjk116 Sep 28 '20 at 16:30
  • 1
    Help yourself by having a look at any stderr output generated. Do this by redirecting stderr to stdout (2>&1), to a separate file; e.g. /usr/bin/python3 /home/projects/temperature_logger/analyze.py >> /home/myusernm/log.txt 2>&1 – Seamus Sep 28 '20 at 21:44
0

Not sure how you are writing scripts.log but for testing if you are using logging.info then make sure you have set the basis config correct for logging... like so...

if __name__ == '__main__':
    logging.basicConfig(filename='test.log',level=logging.DEBUG)
    logging.info("running file!")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.