简体   繁体   English

从Bash脚本运行的Python脚本未记录

[英]Python script ran from Bash Script not logging

I have a bash script that runs among other things a python script every time the system is booted. 我有一个bash脚本,它在每次启动系统时都会运行python脚本。

Part of that python script is supposed to be logging to a log file (mostly used for debug purposes). 该python脚本的一部分应该记录到日志文件中(主要用于调试目的)。 The script runs fine whether I run it manually or through the bash script. 无论是手动运行还是通过bash脚本运行,脚本都可以正常运行。

The issue is that when I run it through the bash script the Python script does not create the log file nor log any of the debugging LOGGING commands I have in the script. 问题是,当我通过bash脚本运行它时,Python脚本不会创建日志文件,也不会记录该脚本中包含的任何调试LOGGING命令。

I am assuming this is a permission issue within the directory. 我假设这是目录内的权限问题。

I'm calling my script from /etc/rc.local 我正在从/etc/rc.local调用我的脚本

sudo /home/user/python_scripts/go.sh &

Here is the python line in my bash script (go.sh).. 这是我的bash脚本(go.sh)中的python行。

#!/bin/sh
python /home/user/python_scripts/go.py

Inside of my python code I am logging using the following (which when I run manually places the log file in the python_scripts directory: 在我的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')

Try using the whole path to the log file. 尝试使用日志文件的整个路径。 It will log into the working directory when the script is run. 运行脚本时,它将登录到工作目录。

If you run from /home/user/python_scripts/ it will put the file there. 如果从/ home / user / python_scripts /运行,它将把文件放在那里。

If you run from /home/ the file will be in your home directory. 如果从/ home /运行,该文件将位于主目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM