0

I have a Python3 program that I need to execute 2 minutes past every hour.

Unable to get python to execute directly from cron, I’ve written a shell script with the commands in it.

The crontab to execute this shell script is this:

*/2 * * * * bash /home/john/PYTHONS/MASTER.sh

The shell script is simple. I’ve tried various versions, including this:

#!/bin/bash
cd /home/john/PYTHONS/
python3 ~/PYTHONS/COMPLEXPYTHON.py
python3 ~/PYTHONS/SIMPLEPYTHON.py

and including this:

#!/bin/bash
cd /home/john/PYTHONS/GDAXDEV/VPUMP
python3 $HOME/PYTHONS/COMPLEXPYTHON.py
python3 $HOME/PYTHONS/SIMPLEPYTHON.py

Here’s the issue:

  1. If I execute the shell script from the command line BOTH python scripts run just fine.

  2. If I execute the shell script from cron, ONLY THE SECOND python script runs. The first one just doesn’t run at all. And, I can’t seem to find out why, or how to fix it.

Any suggestions or help would be greatly appreciated.

3
  • you are running from different directories in each case, this might be the issue. Commented Apr 26, 2018 at 11:22
  • What does which python give you? Do you activate some sort of python environment (virtualenv, conda etc) to run your scripts? Also, please don't run shell scripts with bash <scriptname> instead, make the script executable with chmod +x and just run it directly. I would also get rid of ~ and $HOME in the shell script and use the full path. Commented Apr 26, 2018 at 19:36
  • Thank you Emmet for the tips on shell usage. As a linux newbie this sort of stuff is so helpful. Commented Apr 28, 2018 at 20:03

1 Answer 1

1

I see questions about this issue all around, and no single one of them solved the issue for me. Several hours of testing every combination of variables lead me to a solution that’s reliable.

I’m no longer using a shell script, but invoking python directly in cron.

First, we did this:

$ which python3
/home/john/[path to python]/bin/python3

So, we added the shebang in every one of the python files in the project:

#!/home/john/[path to python]/bin/python3

And, in cron we used the full path to that python3, as well as the full path to the WORKING.py file we wanted to run. We want the thing to run 5 minutes past every hour so:

5 * * * * /home/john/[path to python]/bin/python3 /home/john/[path to dir]/WORKING.py

However, the log files this produced were being placed in /home/john/, and I wanted them in the specific directory. Also, there are a load of additional python files in there – with the same names as python files in nearby directories – so I wanted to be certain that any call gets the right one.

So, I added a CD command at the beginning of the cron command:

5 * * * * cd /home/john/[path to dir]/ && /home/john/[path to python]/bin/python3 /home/john/path to dir]/WORKING.py

It’s been running reliably ever since.

I’m grateful for the resources at stackoverflow and the great and fast help I’ve received in this community, and I hope this entry helps save someone else a whole afternoon of frustration.

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.