0

I have been trying to automatically run a Python script with cron, to understand PATH.

First I try with the user crontab:

crontab -e

I set up crontab like this:

PYTHONPATH=/home/dpa/anaconda3/bin/python
PATH=/home/dpa/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
36 15 * * * python /home/dpa/PycharmProjects/Parser/Parser.py >> /tmp/mycommand.log 2>&1

The script does not run, and I get the following error:

/bin/sh: 1: python: not found

Then I try as superuser:

sudo crontab -e

and I set up cronetab like this:

PYTHONPATH=/home/dpa/anaconda3/bin
PATH=/home/dpa/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin 
43 15 * * * su - dpa python /home/dpa/PycharmProjects/Parser/Parser.py >> /tmp/mycommand.log 2>&1

Now the STDERR looks like this:

-su: python: No such file or directory
4
  • 1
    why are you setting the python module search path PYTHONPATH to a executable or directory where executables go? executables are not modules.
    – thrig
    Commented Jun 18, 2018 at 14:13
  • Thanks for the comment. I did it because I found this guide: pythonadventures.wordpress.com/2012/03/31/…
    – Program
    Commented Jun 18, 2018 at 14:14
  • Did you check the simple things (like /home/dpa/anaconda3/bin/python actually exists - and is not a broken link for example?) Commented Jun 18, 2018 at 16:19
  • @steeldriver, thanks for the input! I checked the path, and it did indeed exist
    – Program
    Commented Jun 18, 2018 at 16:33

1 Answer 1

0

To start with, as you have root access, I would recommend you compile and build anaconda in /opt and not in your home directory.

What you are effectively doing is setting the variable PATH as being equal to that list of directories and PYTHONPATH as being equal to that one directory where the anaconda executables are which is causing your issue.

To get what you want, a better way is to become root and then, assuming your shell is bash:

export PATH=/home/dpa/anaconda3/bin:$PATH
export LD_LIBRARY_PATH=/home/dpa/anaconda3/lib
export PYTHONHOME=/home/dpa/anaconda3
bash

That sets the path and library to look in the directories containing anaconda python and its libraries and bash starts a new session. You can then add the python command to crontab.

If you are going to run it as root then you don't need the su - and it's also better just to run it in the crontab of root as the way you have it, PYTHONPATH and PYTHONHOME are set in your user's environment and not in that of root.

If it's better, you can put it all into a script and just add the script itself to crontab.

Once again, I recommend installing anaconda into /opt and not your user's home directory.

2
  • I ran the commands as you wrote, and for some reason it works now! I still need to learn more about this PATH/PYTHONPATH though. Do you have any good resources to start with?
    – Program
    Commented Jun 18, 2018 at 16:32
  • @Program It's more to do with setting your path and exporting environment variables in general. A simple Google search for "exporting bash variables" and "setting path in Linux" will help you. They all cover everything pretty well so you'll find what you need by going over them and working with them on your machine. To get you started, if it's more convenient, you can add the lines beginning with export to your ~/.bash_profile and then source ~/.bash_profile as root. That will set it permanently so you won't have to do it every time. Commented Jun 18, 2018 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .