0

I've been trying to debug this for a while now and I feel like I've tried everything.

Code is slightly modified with *** for company reasons.

The following executes as expected when run from a session as my local user.

/var/www/****/***/run.sh path_to_my/script.py 2>&1 >> /var/www/****/***/test.log

Where run.sh is just a wrapper for running Python in a virtualenv:

#!/usr/bin/env bash

wd=$(dirname $0)

source ${wd}/virtualenv/bin/activate
python ${wd}/$1

I have placed a print statement inside of the Python main to show that it's being executed.

if __name__ == "__main__":
    print("I got in here...")

When running the command as my local user, the log will contain this printed statement. However, when run in cron as:

*/30 * * * * /var/www/****/***/run.sh path_to_my/script.py 2>&1 >> /var/www/****/***/test.log

I do not get any printed statement, nor do I receive any error output from the 2>&1.

My permissions are 755 on both the .sh and .py scripts.

Everything works as expected except when run via cron.

Am I missing something? Does cron not use .bashrc for the crontab user?

2
  • Related? stackoverflow.com/questions/34967093/… Commented Feb 20, 2019 at 16:49
  • I'm already redirecting stderr and stdout correctly. The question referenced didn't want the error to go to his log. In my case, I do. Commented Feb 20, 2019 at 17:06

1 Answer 1

0

First make sure your local cronjob is running by putting the following in the crontab file and check to see if it get written out at /tmp/env.output after a minute or two

* * * * * env > /tmp/env.output

Second make sure the user running the crontab has permission to write to the /var/www/****/***/test.log file

Third try changing your script to

wd=$(dirname $0)
cd $wd
source activate
python ${wd}/$1

Edited: Anders was able to figure out the answer by himself by adding PYTHONPATH to the cron environment: export PYTHONPATH="${PYTHONPATH}:${wd}"

Sign up to request clarification or add additional context in comments.

3 Comments

I do have the correct permissions for the log, since some other debugging text appears there. In this case, it's the Python that's not running. But your thoughts on env are helpful. Thank you.
That tip helped. PYTHONPATH was missing from the cron env. So I just made my script include export PYTHONPATH="${PYTHONPATH}:${wd}". If you want to edit the answer to include this, I'll select it as the answer. Thanks!
Great that you've figured it out. Cheers!

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.