12

I had issues running my python script on shared hosting (bluehost), and with the help of other SO threads I was able to set PYTHONPATH and run the script with no issues.

Now I need to run the script via a cron job. The cron jobs in shared hosting environment are just one line which I can call the script, but can't figure out how to set PYTHONPATH before calling the script.

Example:

python /path/to/my/script.py

I am sure this issue should be common but I couldn't find any answer in other threads.

Any idea how to set PYTHONPATH for the cron jobs?

Also the codebase is developed in a local environment and the server gets a copy through git pull. So my preferred solution is not to change the source code for the server. It's ok to call another script from cron job which calls the main script and set the variables there, but changing the main script I prefer not to happen so that I don't need to maintain two versions of the code one for local and one for the server.

2 Answers 2

21

Change your cron job to run a shell script. Inside the shell script, set PYTHONPATH and then call the python program.

Change your cron job to this:

/path/to/my_shell_script.sh

Contents of my_shell_script.sh:

export PYTHONPATH=something
python /path/to/py/python/program.py

If you don't want to have a separate shell script, you can cram it all into the cron entry, although it can get very long:

PYTHONPATH=something python /path/to/py/python/program.py
5
  • can also set that variable directly in the crontab file and it will get injected into the environment that the job is run with.
    – reptilicus
    Commented Jul 15, 2016 at 21:48
  • @reptilicus with the shared hosting do we still have access to the crontab file? Right now all I can see is to create single line crons from the cpanel.
    – apadana
    Commented Jul 16, 2016 at 0:30
  • what should "something" be? the path to python?
    – Khan
    Commented Sep 6, 2018 at 23:25
  • 1
    @Khan PYTHONPATH tells python where to look for third-party package modules. If you don't have any such modules, or they are installed in the system default location, then you don't need to set PYTHONPATH. Commented Sep 7, 2018 at 2:12
  • When I have several of these exports, I find it convenient to put them in an external file and source that file in the shell script that cron runs: source "$HOME/.myenv". That way I can also include the shell script in version control without worrying about secrets in the environment variables.
    – Dominik
    Commented Apr 30 at 17:14
-1

You can also use runuser if the PYTHONPATH variable is set in your bashrc or other places environment variables are set on login. For example

 * * * * * root /sbin/runuser -l <USER> -c '<SOME PYTHON SCRIPT>'

will run once per minute under the root user BUT using the login shell for to run the script.

From the runuser man page:

   -, -l, --login
          make the shell a login shell, uses runuser-l PAM file instead of default one

   -c, --command=COMMAND
          pass a single COMMAND to the shell with -c

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