Search code examples
pythonlinuxcronsmtplib

struggle to run python script with crontab


I am currently working on some python scripts that involve sending emails to recipients with information I scrape from the web or by API.

The first script, magic_sea.py scrapes magicseaweed for wave heights in certain locations. The second one uses an API to get weather data.

Both scripts run as they should when I type in the command and a second later I recieve the inteded email.

python3 <script.py>

But only the first one runs when I put them in the crontab like this

* * * * * /usr/bin/python3.5 /path/to/script.py >> crontab_log.txt * * * * * /usr/bin/python3.5 /path/to/second_script.py >> crontab_log.txt

I am using the shebang #!/usr/bin/python3.5 in both scripts

looking at the crontab_log.txt file, they both seem to run fine. But I only recieve emails from the first scripts.


Solution

  • Your method of logging:

    * * * * * /usr/bin/python3.5 /path/to/script.py >> crontab_log.txt
    * * * * * /usr/bin/python3.5 /path/to/second_script.py >> crontab_log.txt
    

    only sends stdout to the log file, not errors. You can fix that by using 2>&1 to send stderr to stdout like this:

    * * * * * /usr/bin/python3.5 /path/to/script.py >> crontab_log.txt 2>&1
    * * * * * /usr/bin/python3.5 /path/to/second_script.py >> crontab_log.txt 2>&1
    

    That should allow you to identify and correct any errors.

    If you still don't see what you need, add logging all through script 2 to see what is happening while it is being executed by cron.