Liondancer Liondancer - 1 year ago 48
Python Question

crontab not fully working. only echo statements being run

I have a job in my crontab to run a script (

) every minute. The script contains

#!/usr/bin/env bash

echo "starting"
/home/sys_bio/username/p35/bin/python3.5 -m qefunctional.qe.tests.prodprobe -p post -j test.json
echo "finised"

When I am in the directory
and I run the commany
, it runs as expected.

However when I add the job to the crontab, only the
portions run.

Another thing to note is
is where my python3 is located. So
is how I run all my python3 scripts.

This is what I have in my crontab

* * * * * /home/sys_bio/username/ >> /home/username/tracer.cron.txt

I am getting the
file created but only the echo statements are being saved.

This may seem confusing but the directory structure is like so:


* edits *

I was able to add a bit more to my script so I can redirect errors to a file. The script now looks like this and I am getting this error:

/home/sys_bio/username/p35/bin/python3.5: Error while finding spec for 'qefunctional.qe.tests.prodprobe' (<class 'ImportError'>: No module named 'qefunctional')


#!/usr/bin/env bash

echo "Starting Tracer POST"

/home/sys_bio/username/p35/bin/python3.5 -m qefunctional.qe.tests.prodprobe -p post -j /home/sys_bio/username/test.json -d -v 2>/home/username/crontab.output

echo "Ending Tracer POST"

Answer Source

Including the path to bash shell might be needed:

* * * * * /bin/sh /home/sys_bio/username/ >> ...

cron otherwise might not really know what to do.

The same principle also applies to what is included in your script. Using relative file names can fail since they are often not interpreted the same as if you were using your local interactive shell (eg):


It might be necessary to use the absolute paths (eg):


EDIT: It's apparent from the error generated by Python the issue is definitely path related. Python is trying to locate the module in the path /home/sys_bio/username/p35/bin/python3.5 instead of two directories up in /home/sys_bio/username/. The fix would be to cd into the directory or by giving the absolute path to the module. By adding the following into the .sh script:

cd /home/sys_bio/username

Should allow the command following it to look for the module there instead of two levels down.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download