[Python] Why are there output differences when running Python code from cron?

Discussão em 'Python' iniciado por Stack, Fevereiro 11, 2025.

  1. Stack

    Stack Membro Participativo

    I have some code which report status of a Pi using inxi. It runs fine from a terminal, but when it runs from cron it adds some characters and misses others.

    This is the code:

    #!/home/pi/.venv/bin/python

    import subprocess

    ret = subprocess.run(['inxi -F'], shell = True, capture_output = True)
    if ret.stderr.decode():
    print(f'Errors = {ret.stderr.decode()}')
    print(f'StdOut = {ret.stdout.decode()}')
    rpt = open('/home/pi/v04_8/inxi.rpt', 'w')
    rpt.write(ret.stdout.decode())
    rpt.close()


    This is the start of the output when run from a terminal:

    System:
    Host: pi-radio Kernel: 6.6.31+rpt-rpi-v7 arch: armv7l bits: 32
    Console: pty pts/0 Distro: Raspbian GNU/Linux 12 (bookworm)
    Machine:
    Type: ARM System: Raspberry Pi 2 Model B Rev 1.1 details: BCM2835
    rev: a01041 serial: 000000000fd84202


    This is that same section when run from cron, note that there is an ascii 3 before the '12' which doesn't show on this page:

    12System:
    12Kernel 6.6.31+rpt-rpi-v7 12arch armv7l 12bits 32 12Console N/A 12Distro Raspbian GNU/Linux 12 (bookworm)
    12Machine:
    12Type ARM 12System Raspberry Pi 2 Model B Rev 1.1 12details BCM2835 12rev a01041 12serial <filter>


    Why does this happen? Thanks Mick

    Continue reading...

Compartilhe esta Página