inxi report problem
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 :: 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: :: Code :: 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 display on this page:: :: Code :: 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 Back to top |
inxi thinks it's in IRC, those are IRC color codes. That's usually the cause when inxi si run via a subshell where inxi can't tell it's in a subshell, which is the case here.
Normally if inxi detects it's being sent to some other output via a pipe or redirect, it disables colors automatically, but in cases like yours it can't tell, so it just outputs the color codes it thinks are right for the task. Sample of normal detection and switching off of colors: :: Code :: echo "$(inxi -A)"
Audio: Device-1: C-Media CMI8788 [Oxygen HD Audio] driver: snd_virtuoso Device-2: Advanced Micro Devices [AMD/ATI] Cedar HDMI Audio [Radeon HD 5400/6300/7300 Series] driver: snd_hda_intel Device-3: Advanced Micro Devices [AMD] Family 17h HD Audio driver: snd_hda_intel API: ALSA v: k6.11.5-1-liquorix-amd64 status: kernel-api Server-1: PulseAudio v: 16.1 status: active If memory serves, when inxi starts it tries to determine the start client after first confirming it's not in tty, but since IRC has python start clients, once it determined that the start client was an unknown python, it assumes it's in IRC, and switches to IRC colors. If you'd posted the full -F output, you'd see in the Info line start client probably says 'unknown'. There's a whitelist of known start clients that forces to tty no color mode, but an individual script is never going to be on that list, so you got IRC colors. Since this is known, it's handled, so you can use either -c0 for no colors, or --tty, to force tty behavior. Both turn off subshell output colors that are being captured by a non shell environment. --tty is preferred in your case since IRC output differs in a few other ways than color codes. Technically you don't "have some code" that does this, you have a small program that does this, and inxi doesn't know anything about that, nor can it ever know about individual user scripts like that, so the --ty switch off options exist. --tty was added specifically for cases like yours, but just test them both. Note that if inxi still outputs colors, only tty colors now, you will have to also add -c0 to explicitly turn off colors, but --tty alone usually works. :: Code :: --tty Forces irc flag to false. Generally useful if pinxi is running
inside of another tool like Chef or MOTD and returns corrupted color codes. Please see man page or file an issue if you need to use this flag. Must use -y [width] option if you want a specific output width. Always put this option first in an option list. See -Z for disabling output filters as well. You're basically presenting inxi with an environment it has no idea what to do about, so you have to tell it what you want it to do. I believe the comment about using -y is legacy, since inxi in tty defaults to 80 column width output, and -y is useful if you want a different width. You can also tweak the indentations and other output features, the man and help explain this stuff. Back to top |
Sorry I was late to respond, I missed this post initially.
Back to top |
All times are GMT - 8 Hours |