当由cron作业启动时,将print python输出重定向到文件

时间:2017-04-27 14:38:37

标签: python linux cron output

Cron行:

@reboot /usr/bin/python /home/pi/main.py & >> /home/pi/log.txt 2>&1
@reboot whoami >> log2.txt
  

两个Skripts开始了!
  这两个文件都已创建!

在log2.txt中代表用户pi。 但是log.txt保持空白。当我通过控制台启动python脚本时,python脚本的输出将显示在控制台上。

有什么问题,输出没有写入文件?

我也尝试过root的cronjob,结果相同

2 个答案:

答案 0 :(得分:0)

更改为。

/ usr / local / python /home/pi/main.py 2>& 1>> /home/pi/log.txt

答案 1 :(得分:0)

def log(who, type, debug_level, msg, ip = ""):
if (debug_level <= DEBUG):
    with open('/home/pi/log.txt','a+') as f:
        if (ip != ""):            
            f.write(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " - " + who + " - " + ip + ": " + type + msg + '\n')
        f.write(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " - " + who + " - " + type + msg + '\n')

我将直接写入日志功能中的文件。

相关问题