-2

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.

3
  • Can you please try to cleanup the layout for the error output? Looks like the line wrapping is wrong. It's very difficult to read. Jul 8, 2011 at 18:18
  • @Stefan_Lasiewski - that is the error message...one large piece that goes to my log file. What do you want me to do to it?
    – rich
    Jul 8, 2011 at 18:34
  • That looks like a typical Python traceback to me. That is, I think the formatting is correct and there's not much he can do to fix it.
    – larsks
    Jul 8, 2011 at 19:04

1 Answer 1

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?

9
  • my shell script redirects all output to a log file. That is were I am getting the trackback. Looks like Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) interactively. Yes I did install it via MacPorts. What do you suggest my next move be?
    – rich
    Jul 8, 2011 at 19:21
  • 1
    Make sure the directory containing the python 2.6 binary is in your PATH when running your shell script. The simplest solution is to explicitly set your PATH variable in your shell script to the same value that you see when typing echo $PATH interactively. You can also explicitly call python 2.6 (e.g., type /opt/local/bin/python somescript.py) instead of relying on PATH.
    – larsks
    Jul 8, 2011 at 19:27
  • I don't think I can specify the py file because my schell script is calling the linkchecker which I have no idea what that uses. I put $PATH=/opt/local/bin:/opt/local/sbin:/sw/bin:/sw/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/\ bin:/usr/X11R6/bin like it says when I say echo $PATH interactively. When I run the cron env prints out.....SHELL=/bin/sh USER=keith PATH=/usr/bin:/bin PWD=/Users/keith HOME=/Users/keith SHLVL=2 LOGNAME=keith _=/usr/bin/env
    – rich
    Jul 8, 2011 at 19:33
  • I think you missed my comment about setting PATH explicitly. If you're successfully running Python out of MacPorts then your path is something other than PATH=/usr/bin:/bin. What do you see if you type echo $PATH at your shell prompt? What do you see if you type which python?
    – larsks
    Jul 8, 2011 at 19:35
  • I see: /opt/local/bin:/opt/local/sbin:/sw/bin:/sw/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/X11R6/bin ... I put $PATH=/opt/local/bin:/opt/local/sbin:/sw/bin:/sw/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/\ bin:/usr/X11R6/bin at the top of my shell script too
    – rich
    Jul 8, 2011 at 19:35

Not the answer you're looking for? Browse other questions tagged .