1

我有以下 cronjob:

0 0 * * * source /home/admin/data/virtualenvs/myapp/bin/activate && python /home/admin/data/virtualenvs/myapp/project/manage.py 1c-import >> /home/admin/data/logs/1c-import.log 2>&1

因此,在日志文件中,我看到以下错误:

Traceback (most recent call last):
  File "/home/admin/data/virtualenvs/myapp/project/manage.py", line 28, in <module>
    execute_from_command_line(sys.argv)
  File "/home/admin/data/virtualenvs/myapp/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/home/admin/data/virtualenvs/myapp/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/admin/data/virtualenvs/myapp/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/admin/data/virtualenvs/myapp/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/home/admin/data/virtualenvs/myapp/project/helper/management/commands/1c-import.py", line 94, in handle
    print 'Product [%s] with price %s and qty %s' % (product_title, product_price, product_qty)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 34-35: ordinal not in range(128)

但是当我尝试手动运行这个命令时 - 没关系。你怎么看 - 什么可能导致这个错误?

4

2 回答 2

4

问题是 cron 的语言环境设置不正确。

/etc/environment集合

LANG=en_US.UTF-8

此链接将很有用:cron 作业的语言环境设置

还有这个问题:Python3: UnicodeEncodeError only when run from crontab

于 2013-05-16T11:17:14.193 回答
1

我的猜测是,当从 cron 作业运行它时,系统会尝试将您的产品名称、价格和数量转换为 ASCII,而当您手动运行它时它会选择不同的编码。您的一种或多种产品的名称、价格和/或数量中将包含无法编码为 ASCII 的字符。

如果您打印这样的信息有帮助吗?:

ENCODING = "utf-8"
print 'Product [%s] with price %s and qty %s' % (product_title.encode(ENCODING), 
                      product_price.encode(ENCODING), product_qty.encode(ENCODING))
于 2013-05-16T10:25:37.060 回答