shell script doesn't run correctly in cron [closed]

I want to call a shell script I wrote from a cron. The shell script uses the program Linkchecker. If I run the shell script from the terminal it works just fine. However, when cron runs I get the following errors:

File "/Users/keith/etc/cron/linkchecker", line 40, in from linkcheck.director import console, check_urls, get_aggregate
File "/opt/local/lib/python2.5/site-packages/linkcheck/director/init.py", line 25, in from . import aggregator, console File "/opt/local/lib/python2.5/site-packages/linkcheck/director/aggregator.py", line 25, in from . import logger, status, checker, cleanup File "/opt/local/lib/python2.5/site-packages/linkcheck/director/status.py", line 20, in from . import task File "/opt/local/lib/python2.5/site-packages/linkcheck/director/task.py", line 20, in from . import console File "/opt/local/lib/python2.5/site-packages/linkcheck/director/console.py", line 27, in stderr = codecs.getwriter(i18n.default_encoding)(sys.stderr, errors="ignore") File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/codecs.py", line 930, in getwriter return lookup(encoding).streamwriter LookupError: unknown encoding: Traceback (most recent call last):
File "/Users/keith/etc/cron/linkchecker", line 40, in from linkcheck.director import console, check_urls, get_aggregate
File "/opt/local/lib/python2.5/site-packages/linkcheck/director/init.py", line 25, in from . import aggregator, console File "/opt/local/lib/python2.5/site-packages/linkcheck/director/aggregator.py", line 25, in from . import logger, status, checker, cleanup File "/opt/local/lib/python2.5/site-packages/linkcheck/director/status.py", line 20, in from . import task File "/opt/local/lib/python2.5/site-packages/linkcheck/director/task.py", line 20, in from . import console File "/opt/local/lib/python2.5/site-packages/linkcheck/director/console.py", line 27, in stderr = codecs.getwriter(i18n.default_encoding)(sys.stderr, errors="ignore") File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/codecs.py", line 930, in getwriter return lookup(encoding).streamwriter LookupError: unknown encoding: Traceback (most recent call last):
File "/Users/keith/etc/cron/linkchecker", line 40, in from linkcheck.director import console, check_urls, get_aggregate
File "/opt/local/lib/python2.5/site-packages/linkcheck/director/init.py", line 25, in from . import aggregator, console File "/opt/local/lib/python2.5/site-packages/linkcheck/director/aggregator.py", line 25, in from . import logger, status, checker, cleanup File "/opt/local/lib/python2.5/site-packages/linkcheck/director/status.py", line 20, in from . import task File "/opt/local/lib/python2.5/site-packages/linkcheck/director/task.py", line 20, in from . import console File "/opt/local/lib/python2.5/site-packages/linkcheck/director/console.py", line 27, in stderr = codecs.getwriter(i18n.default_encoding)(sys.stderr, errors="ignore") File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/codecs.py", line 930, in getwriter return lookup(encoding).streamwriter LookupError: unknown encoding: Traceback (most recent call last):
File "/Users/keith/etc/cron/linkchecker", line 40, in from linkcheck.director import console, check_urls, get_aggregate
File "/opt/local/lib/python2.5/site-packages/linkcheck/director/init.py", line 25, in from . import aggregator, console File "/opt/local/lib/python2.5/site-packages/linkcheck/director/aggregator.py", line 25, in from . import logger, status, checker, cleanup File "/opt/local/lib/python2.5/site-packages/linkcheck/director/status.py", line 20, in from . import task File "/opt/local/lib/python2.5/site-packages/linkcheck/director/task.py", line 20, in from . import console File "/opt/local/lib/python2.5/site-packages/linkcheck/director/console.py", line 27, in stderr = codecs.getwriter(i18n.default_encoding)(sys.stderr, errors="ignore") File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/codecs.py", line 930, in getwriter return lookup(encoding).streamwriter LookupError: unknown encoding: Traceback (most recent call last):
File "/Users/keith/etc/cron/linkchecker", line 40, in from linkcheck.director import console, check_urls, get_aggregate
File "/opt/local/lib/python2.5/site-packages/linkcheck/director/init.py", line 25, in from . import aggregator, console File "/opt/local/lib/python2.5/site-packages/linkcheck/director/aggregator.py", line 25, in from . import logger, status, checker, cleanup File "/opt/local/lib/python2.5/site-packages/linkcheck/director/status.py", line 20, in from . import task File "/opt/local/lib/python2.5/site-packages/linkcheck/director/task.py", line 20, in from . import console File "/opt/local/lib/python2.5/site-packages/linkcheck/director/console.py", line 27, in stderr = codecs.getwriter(i18n.default_encoding)(sys.stderr, errors="ignore") File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/codecs.py", line 930, in getwriter return lookup(encoding).streamwriter LookupError: unknown encoding: Traceback (most recent call last):
File "/Users/keith/etc/cron/linkchecker", line 40, in from linkcheck.director import console, check_urls, get_aggregate
File "/opt/local/lib/python2.5/site-packages/linkcheck/director/init.py", line 25, in from . import aggregator, console File "/opt/local/lib/python2.5/site-packages/linkcheck/director/aggregator.py", line 25, in from . import logger, status, checker, cleanup File "/opt/local/lib/python2.5/site-packages/linkcheck/director/status.py", line 20, in from . import task File "/opt/local/lib/python2.5/site-packages/linkcheck/director/task.py", line 20, in from . import console File "/opt/local/lib/python2.5/site-packages/linkcheck/director/console.py", line 27, in stderr = codecs.getwriter(i18n.default_encoding)(sys.stderr, errors="ignore") File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/codecs.py", line 930, in getwriter return lookup(encoding).streamwriter LookupError: unknown encoding:

I have chmod 777 the linkchecker file. I have moved the linkchecker file to my current home directory. I added the cron by saying

crontab -e

I put in

38       12      *       *       5      /Users/keith/etc/cron/linkchecker.sh

The first line of my shell script is #!/bin/sh

So the only thing I can think of is I'm missing some environment variables when cron runs. When I add env at the top of my script and compare the variables from when cron runs versus from the terminal there is a difference. According to https://stackoverflow.com/questions/1694942/environment-variable-used-in-shell-script-appear-blank-in-log-file-when-run-by-cr I can add . /etc/profile and that should put in the correct environment variables. This does not appear to be sucking in the variables because the variables are still the same when I use env.

I am on a Mac if that makes any difference. Also I pasted in the working variables at the top of my script that worked when I run from terminal and cron still didn't work.


Solution 1:

How are you getting the traceback? The cron entry you've posted doesn't seem to be redirecting output to a file.

Are you using a different version of Python when you run the script interactively? This can happen if you're using, e.g., MacPorts and you've got a newer version of Python there than is installed by OS X. If that traceback comes from your cron job, it appears to be running 2.5. Is this what you end up with when you type python interactively?

Can you show us the script?