0

I tried to automate the launch of a python script. After having tested it works in thonny, I moved to Cron and I got quite lost.

I started from calling 12 21 * * * /usr/bin/python3 /home/pi/main.py to 12 21 * * * /usr/bin/python3.5 /home/pi/main.py(with minute and hour value to change accordingly to the time of test) but nothing happens.

I ultimately tried to add the following line: 12 21 * * * (echo "hi" >>/var/log/it_runs.txt) and that indeed print an "hi" in a "it_runs" txt file. That makes me think the problem is with calling python. Do I call python in the wrong way? When debugging with Thonny i set python as 3.5.

Pls and advice is more than accepted Ale

4
  • There could be lots of reasons. But since you already know how to log console output from a cronjob, why not do the same for your main.py script and see what the log tells you ?
    – Kate
    May 12, 2021 at 20:43
  • I tried again many times today. I found out that it runs smoothly when on terminal
    – mant0
    May 12, 2021 at 20:59
  • What happens when you run /usr/bin/python3 /home/pi/main.py from your CLI (the terminal)?
    – Seamus
    May 14, 2021 at 9:05
  • It runs smoothly
    – mant0
    May 14, 2021 at 11:02

1 Answer 1

2

You didn't tell us what your Python script did - or was supposed to do. We try to answer RPi-related questions here, but all of our mind-readers are currently on strike for better wages. So you're left to depend on people like me who need some information to provide answers.

All that said, I'm going to take a guess. I'll answer your question in this way - by a working example that is as similar to yours as I know how to make it:

  1. Create a Python script in user pi's home directory;

    $ nano hello.py
    
  2. Enter a single line in the editor:

    print("Hello from Python Script Launched run under cron")
    
  3. Save & close the editor - check to ensure the file /home/pi/hello.py exists

  4. Open your crontab for editing:

    $ crontab -e
    
  5. Make the following entry at the bottom of your crontab:

    * * * * * /usr/bin/python3 /home/pi/hello.py >> hellopy.txt 2>&1
    
  6. Save & exit the crontab editor - it should confirm that a new crontab has been installed

  7. You can see the once-per-minute output as it goes to the output file using tail:

    $ $ tail -f -n 10 hellopy.txt
    Hello from Python Script Launched run under cron
    Hello from Python Script Launched run under cron
    etc, etc, etc
    

    control + c to end the listing (or just use cat instead of tail if you prefer)


EDIT/ADDENDUM:

To view your user environment:

$ printenv

To view your "cron user's environment", add the following line to your crontab:

@reboot sleep 10; /usr/bin/printenv > /home/pi/cronenv.txt 2>&1

Review the differences between these two environments in light of your Python program.

10
  • Dear Seamus, thank you for your answer. My python script is supposed to get rain precipitation data from a certain region and post a tweet. Indeed, it works when launching the script from terminal. but nothing happens when using cron.
    – mant0
    May 14, 2021 at 11:04
  • @mant0: Have you tried the steps I outlined in the answer?
    – Seamus
    May 14, 2021 at 17:37
  • yes, but without any improvement:(
    – mant0
    May 14, 2021 at 19:58
  • @mant0: I don't see how to help you without more information on your Python script and your crontab listing. It's clear that the "approach" is sound and correct. ... As another guess, could it be that when you run this script from the terminal it prompts you for input? Or perhaps it is using resources or environment variables that aren't available to cron? I understand you're not deliberately withholding information, but you need to understand that you've not given us enough to do anything more than guess at the problem - and I hate guessing.
    – Seamus
    May 14, 2021 at 20:16
  • I do understand your point of view. However, de facto, I am trying all I can with no solution. After installing the new OS on raspberry pi, I re-install every lib i had in the past. Again, the same problem occur. I can run the very same command from terminal and it runs smoothly. Funny thing, using any other python script is fine.
    – mant0
    May 19, 2021 at 8:37

Your Answer

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

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