可能重复:
shell脚本在cron中不能正常运行
我想调用一个我从cron写的shell脚本。 shell脚本使用程序Linkchecker。 如果我从terminal运行shell脚本,它工作得很好。 但是,当cron运行时,我得到以下错误:
从linkcheck.director import console,check_urls,get_aggregate中的第40行的文件“/ Users / keith / etc / cron / linkchecker”
文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/init.py”,第25行,来自。 导入聚合器,控制台文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/aggregator.py”,第25行中的from。 导入logging器,状态,检查器,清理文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/status.py”,第20行中的from。 导入任务文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/task.py”,第20行中的from。 导入控制台文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/console.py”,第27行,在stderr = codecs.getwriter(i18n.default_encoding)(sys.stderr,errors =“忽略“)在getwriter返回查找(编码).streamwriter LookupError:未知编码的文件”/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/codecs.py“,行930,回溯(最近一次通话最后):
从linkcheck.director import console,check_urls,get_aggregate中的第40行的文件“/ Users / keith / etc / cron / linkchecker”
文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/init.py”,第25行,来自。 导入聚合器,控制台文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/aggregator.py”,第25行中的from。 导入logging器,状态,检查器,清理文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/status.py”,第20行中的from。 导入任务文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/task.py”,第20行中的from。 导入控制台文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/console.py”,第27行,在stderr = codecs.getwriter(i18n.default_encoding)(sys.stderr,errors =“忽略“)在getwriter返回查找(编码).streamwriter LookupError:未知编码的文件”/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/codecs.py“,行930,回溯(最近一次通话最后):
从linkcheck.director import console,check_urls,get_aggregate中的第40行的文件“/ Users / keith / etc / cron / linkchecker”
文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/init.py”,第25行,来自。 导入聚合器,控制台文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/aggregator.py”,第25行中的from。 导入logging器,状态,检查器,清理文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/status.py”,第20行中的from。 导入任务文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/task.py”,第20行中的from。 导入控制台文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/console.py”,第27行,在stderr = codecs.getwriter(i18n.default_encoding)(sys.stderr,errors =“忽略“)在getwriter返回查找(编码).streamwriter LookupError:未知编码的文件”/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/codecs.py“,行930,回溯(最近一次通话最后):
从linkcheck.director import console,check_urls,get_aggregate中的第40行的文件“/ Users / keith / etc / cron / linkchecker”
文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/init.py”,第25行,来自。 导入聚合器,控制台文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/aggregator.py”,第25行中的from。 导入logging器,状态,检查器,清理文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/status.py”,第20行中的from。 导入任务文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/task.py”,第20行中的from。 导入控制台文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/console.py”,第27行,在stderr = codecs.getwriter(i18n.default_encoding)(sys.stderr,errors =“忽略“)在getwriter返回查找(编码).streamwriter LookupError:未知编码的文件”/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/codecs.py“,行930,回溯(最近一次通话最后):
从linkcheck.director import console,check_urls,get_aggregate中的第40行的文件“/ Users / keith / etc / cron / linkchecker”
文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/init.py”,第25行,来自。 导入聚合器,控制台文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/aggregator.py”,第25行中的from。 导入logging器,状态,检查器,清理文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/status.py”,第20行中的from。 导入任务文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/task.py”,第20行中的from。 导入控制台文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/console.py”,第27行,在stderr = codecs.getwriter(i18n.default_encoding)(sys.stderr,errors =“忽略“)在getwriter返回查找(编码).streamwriter LookupError:未知编码的文件”/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/codecs.py“,行930,回溯(最近一次通话最后):
从linkcheck.director import console,check_urls,get_aggregate中的第40行的文件“/ Users / keith / etc / cron / linkchecker”
文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/init.py”,第25行,来自。 导入聚合器,控制台文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/aggregator.py”,第25行中的from。 导入logging器,状态,检查器,清理文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/status.py”,第20行中的from。 导入任务文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/task.py”,第20行中的from。 导入控制台文件“/opt/local/lib/python2.5/site-packages/linkcheck/director/console.py”,第27行,在stderr = codecs.getwriter(i18n.default_encoding)(sys.stderr,errors =“忽略“)在getwriter返回查找(编码).streamwriter LookupError:未知编码的文件”/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/codecs.py“,行930,
我有chmod 777 linkchecker文件。 我已经将链接检查器文件移动到当前的主目录。 我说,加了cron
crontab -e
我放进去
38 12 * * 5 /Users/keith/etc/cron/linkchecker.sh
我的shell脚本的第一行是#!/ bin / sh
所以我唯一能想到的是当cron运行时我缺less一些环境variables。 当我在脚本的顶部添加env时,比较cron运行时和terminal运行时的variables是有区别的。 根据https://stackoverflow.com/questions/1694942/environment-variable-used-shell-script-appear-blank-in-log-file-when-run-by-cr我可以添加。 / etc / profile并且应该放入正确的环境variables。 这似乎并没有吸引variables,因为当我使用env时variables仍然是相同的。
我在Mac上,如果这有什么区别。 另外我粘贴在脚本顶部的工作variables,当我从terminal和cron运行仍然无法正常工作。
尝试这个
38 12 * * 5 /bin/bash -l -c /Users/keith/etc/cron/linkchecker.sh
-l选项将获得执行脚本的login环境。