2

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

5
  • 2
    What's suspicious is that the ASCII codes are always surrounding the words that are colored when calling inxi through a regular terminal call. Maybe try to adjust the color settings for inxi? For example, try inxi -c 0 -F rather than inxi -F to turn off coloring. Commented Feb 11 at 12:39
  • 1
    Just tried it without colour and it fixes the display, so no ascii 3, but there are still parts missing. terminal run starts 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) but cron run starts System: Kernel: 6.6.31+rpt-rpi-v7 arch: armv7l bits: 32 Console: N/A Distro: Raspbian GNU/Linux 12 (bookworm) Commented Feb 11 at 14:04
  • 1
    inxi likely produces different output when not writing to a terminal, and cron won't provide a terminal as inxi's standard output. Commented Feb 11 at 14:52
  • 2
    You can trick an application into thinking its stdout is a terminal: stackoverflow.com/questions/1401002/… You could apply that trick to inxi here. Commented Feb 11 at 15:08
  • Aside: subprocess.run(['inxi -F'], shell = True, capture_output = True) should be subprocess.run(['inxi', '-F'], capture_output = True) There's no need to use the shell. Commented Feb 11 at 17:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.