Crontab not running my python script

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 11:14:44

What happens when you type

/home/me/project/myscript.py into the shell?

Can you explicitly use /usr/bin/python in your crontbb command?

Can you either use an absolute path to your test.db or cd to the correct directory then execute your python script?

This is helpful to have debug statements in your python and log some data. Crontab can be very tricky to debug.

shargors

It is possible that the script does not start because it cannot locate the python interpreter. Crontab environment may be very different from the shell environment which you are using. The search paths may be differ significantly. Also, you test your script by starting the python interpreter explicitly while you expect the crontab to only start the script. I put this line at the top of my python scripts:

\#!/bin/env python

This line will help locate the interpreter regardless of which directory it is installed in as long as it is in the search path.

There are a lot of half answers across the internet so I thought I would capture this to save someone else some time.

First, cronjob does a poor job of telling you where this is failing. I recommend sending stderr output to a log file like this:

Crontab Command:

# m h  dom mon dow   command
* * * * * /path/to/your_file.sh >> out.txt  2>&1

As this is likely running the command as user, check home directory for the log file. Note this script runs every minute which is good for debugging.

The next issue is you probably have a path problem... as script likely is trying to execute from your home directory. This script sets the current directory, echos it to file, and then runs your program.

Try this :

Script File

#!/bin/sh
cd "$(dirname "$0")";
CWD="$(pwd)"
echo $CWD
python your_python_file.py

Hope this saves someone else some debugging time!!!

Typically, crontab problems like this are caused by the PATH environment variable being more restrictive/different than what your normal user's PATH environment is. Since your shell uses the PATH environment to find the executable (e.g. /usr/bin/python is found in /usr/bin when you type "python" at a shell prompt), when the PATH is missing common locations, like /usr/bin or /usr/sbin, your cron job will fail. This has bit me many times. The simple fix is just to explicitly set the PATH yourself near the top of your crontab file, before any commands that need it. So, just edit the crontab as usual and add something like this near the top (if you binary is not in one of the below paths, you'll need to add it after a colon):

PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

Alternately, just use absolute paths to your binaries and scripts in crontab.

I'd got the same problem. Despite the fact that the script executed manually was working, in crontab no options mentioned above were working at all. I've moved my script from /home/user/script_directory/ to /opt/scripts/ and it started to work. Possible cause of the problem should be the access (read) permissions to subfolder located in home directory.

Try this

* * * * * cd <directory_where_python_file_is> && bin/app etc/app_defaults.yaml

There is some path issue with cron. So when you move to directory with python file, cron works like charm!

If you are using anaconda for python then the path to use will be :

/home/username/anaconda3/bin/python test.py

Easiest way to handle this is to add your python installation's path to PATH in top of the shell script. Something like:

#!/usr/bin/env bash
export PATH="{path to your python installation}:$PATH"
python {python_file_name}.py

As @Shargors said you can test it by

env -i /bin/bash --noprofile --norc

It's usually because the python used by crontab is different from the one you use in the shell. The easiest way to solve this is:

  1. get the python you use in the shell:

$ which python # it may be "python3" or something else /usr/bin/python

  1. use that specific python in crontab file:

* * * * * /usr/bin/python test.py

Also want to mention that using env -i /bin/bash --noprofile --norc in the shell lets you have the same environment as the one used by crontab, and this is super helpful to debug.

Try to put in your crontab:

* * * * * python /path/to/your/script.py

rather than

* * * * * /path/to/your/script.py

Also the shebang line is #!/usr/bin/env python in some environments. env is an executable, and you have to know where it lives with "$ which env".

Veerabahu
  • Is the cron user (where the script fails) and the terminal user (when the script succeeds) are same ?
  • Can you redirect the job output to some file as mentioned in Cron Job Log - How to Log?. We could see whether that helps.
Minnow

While the answers here clearly delineate the problem and solution, I wanted to add another answer which helped me.

If your python script is calling a database, then be sure you can connect to the db properly within the cron env (to identify the cron env--> https://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work). I had a file that would run from the shell, but not as a crontab unless I connected to the database as root from within the python script.

Sometimes I am facing same problem. Whatever I try something as advised here, I may not get result.

So I begin to write "trigger" bash script as follow (let's name it trigger.sh):

#!/bin/bash

/full_path/python_script.py

And I am calling trigger.sh from crontab and everything is fine.

EDIT: Of course, don't forget to do following (give execution right):

$chmod +x python_script.py
$chmod +x trigger.sh
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!