【问题标题】:Python script ran from Bash Script not loggingPython 脚本从 Bash 脚本运行而不记录
【发布时间】: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


【解决方案1】:

尝试使用日志文件的完整路径。脚本运行时会登录到工作目录。

如果你从 /home/user/python_scripts/ 运行,它会将文件放在那里。

如果您从 /home/ 运行,该文件将位于您的主目录中。

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 2013-03-14
    • 1970-01-01
    • 2017-01-11
    • 2012-11-24
    • 2017-11-04
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多