Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a script that runs nightly, that is returning different results than when I run it manually. I realise that this is a common question and my problem most likely lies in an environment variable that isn't picked up when running cron.

I have added my PATH manually to the start of the crontab, and I have added the line

. ~/.bash_profile

to the start of the script. What else could be happening?

share|improve this question

migrated from stackoverflow.com Dec 15 '12 at 0:11

This question came from our site for professional and enthusiast programmers.

    
I'm not sure I can post the script.. It runs a few tests, which outputs the results to a html file, concatenates the html file, and emails it to me. –  confusified Dec 14 '12 at 17:16

1 Answer 1

At least you've tried.

I agree that its most likely an environment problem, and the best way to see what environment your're really working with is to capture the environments to files and then diff them, so .. add

set > /tmp/myEnvrionrment.$$

right after your

. ~/.bash_profile

edit

(The .$$ variable appended to the /tmp filename is the Shell's variable for "current process id", and hence is a relatively unique number, that should be different for each time you run your script.J)

Run the script from cmd line, then from cron

then

diff /tmp/myEnvrionemnt.*

Assuming that there are only two files there, otherwise, look at the timestamps on those files and delete others so you only have 2 files, 1 representing the cmd-line run and the other from the crontab run.

as set shows local variables (which should include exported variables), this should work. If you get identical lists in both set output files, then try

export > /tmp/myExportedVars.$$

as above, and try diffing those.

Look not only at PATH, but LD_* vars and LANG vars.

IHTH

share|improve this answer
    
Will try that and get back to you, but in the meantime, does the .$$ do something special? –  confusified Dec 14 '12 at 17:28
    
see my edit. Good luck. –  shellter Dec 14 '12 at 17:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.