recherche

Maison  >  Questions et réponses  >  le corps du texte

Java调用python脚本,脚本日志如何输入到日志文件中?如何实时获取脚本日志?

Java调用python脚本遇到的两个问题,求教:

相关代码和脚本

终端直接执行,会生成日志文件
python /tmp/pytest.py >>/tmp/pylog.log 2>&1
Java调用,不会新建生成日志文件

···
Runtime.getRuntime().exec(“python /tmp/pytest.py >>/tmp/pylog.log 2>&1”)
···

python脚本
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import logging
import time

logging.basicConfig(level=logging.DEBUG)

for num in range(0, 3):
    time.sleep(1)
    logging.info('logging 当前序号 :' + str(num) )
    print' print当前序号 :', num
怪我咯怪我咯2862 Il y a quelques jours1354

répondre à tous(2)je répondrai

  • ringa_lee

    ringa_lee2017-04-18 10:24:33

    N'utilisez pas le caractère de redirection dans Runtime.exec(), utilisez plutôt process.getInputStream() pour obtenir le journal. Par exemple :

        Process process = Runtime.getRuntime().exec("python /tmp/pytest.py");
        try (FileOutputStream out = new FileOutputStream("/tmp/pylog.log")) {
            Streams.copy(process.getInputStream(), out);
        }
    

    répondre
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:24:33

    1 Créer un fichier sh
    cd /tmp && echo "/usr/bin/python /tmp/pytest.py >>/tmp/pylog.log 2>&1" >> 🎜>2 Exécuter le fichier sh en java
    Execute Runtime.getRuntime().exec(“/usr/bin/sh /tmp/pytest.sh”) en java
    3 Résolu.

    PS : N'oubliez pas d'utiliser des adresses absolues pour les commandes python et sh.

    Mes terminaux sont /usr/bin/python et /usr/bin/sh. N'oubliez pas d'ajuster la réponse de votre propre terminal.

    répondre
    0
  • Annulerrépondre