-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.

rich
  • 1
  • 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. – Stefan Lasiewski Jul 08 '11 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 08 '11 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 08 '11 at 19:04

1 Answers1

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?

larsks
  • 43,623
  • 14
  • 121
  • 180
  • 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 08 '11 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 08 '11 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 08 '11 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 08 '11 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 08 '11 at 19:35
  • Can you show us the script? If you actually put `$PATH=...` then you've got a syntax error (you need to drop the `$` when your assigning to a variable), and you may need to explicitly `export` the variable (`export PATH`). – larsks Jul 08 '11 at 19:40
  • ah ok sorry I forgot to take off the $ when declaring the variable. Now I am getting the correct $PATH but I'm still getting the same error now. Looks like the PATH didn't do it. I actually pasted in all the variables too that I get when I run interactively and I still have these error messages – rich Jul 08 '11 at 19:44
  • And export $PATH didn't work either – rich Jul 08 '11 at 19:46
  • Since I have yet to see your script I'm going to pass on this one. – larsks Jul 09 '11 at 01:22