【发布时间】:2014-02-13 18:26:19
【问题描述】:
我有一个 bash 脚本,它在每次系统启动时都会运行一个 python 脚本。
该 python 脚本的一部分应该记录到日志文件(主要用于调试目的)。无论我是手动运行还是通过 bash 脚本运行,该脚本都运行良好。
问题是,当我通过 bash 脚本运行它时,Python 脚本不会创建日志文件,也不会记录我在脚本中的任何调试 LOGGING 命令。
我假设这是目录中的权限问题。
我从 /etc/rc.local 调用我的脚本
sudo /home/user/python_scripts/go.sh &
这是我的 bash 脚本 (go.sh) 中的 python 行..
#!/bin/sh
python /home/user/python_scripts/go.py
在我的 python 代码中,我使用以下内容进行记录(当我手动运行时,会将日志文件放在 python_scripts 目录中:
import logging
daily_log = 'log-' + str(time.strftime("%m-%d-%y")) + '.log'
logging.basicConfig(format='%(asctime)s (%(levelname)s) - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', filename=daily_log,level=logging.DEBUG)
logging.debug(' Debug Message - Error 1241')
【问题讨论】:
-
把你写到文件的部分贴出来。
-
bash脚本如何运行? Python 脚本是写入特定文件,还是仅写入标准错误和/或输出(它继承自bash脚本,并且可能与手动运行 Python 时不同)? -
更新了更多信息...
-
您查看this question 的答案了吗?具体来说,将记录器设置为
logging.StreamHandler(sys.stdout)
标签: python linux bash shell logging