systemctl non sembra avere un meccanismo per specificare quando colorare l'output. Una soluzione rapida sarebbe shim isatty(3) per restituire sempre true, ingannando così systemctl nel pensare che stdout sia interattivo. Vale a dire che potresti fare:
# echo "int isatty(int fd) { return 1; }" | gcc -O2 -fpic -shared -ldl -o isatty.so -xc -
# LD_PRELOAD=./isatty.so watch -n300 --color systemctl status plexmediaserver
Il -xc - alla fine del gcc comando dice a gcc per compilare il codice C (-xc ) da stdin (- ). Il resto dei flag indica gcc per creare un file oggetto condiviso denominato isatty.so . Nota che questo potrebbe benissimo danneggiare altri programmi che si basano su isatty restituire un valore legittimo. Tuttavia sembra andare bene per systemctl come isatty sembra essere utilizzato esclusivamente allo scopo di determinare se deve colorare il suo output.
watch -c SYSTEMD_COLORS=1 systemctl status icinga2
man systemd dice
$SYSTEMD_COLORS
Controls whether colorized output should be generated.
ad esempio, puoi forzare la modalità colore con quella.
Sulla base della risposta di @KarlC, ecco uno script che genera e quindi include la libreria in fase di esecuzione:
#!/bin/bash
set -euo pipefail
function clean_up {
trap - EXIT # Restore default handler to avoid recursion
[[ -e "${isatty_so:-}" ]] && rm "$isatty_so"
}
# shellcheck disable=2154 ## err is referenced but not assigned
trap 'err=$?; clean_up; exit $err' EXIT HUP INT TERM
isatty_so=$(mktemp --tmpdir "$(basename "$0")".XXXXX.isatty.so)
echo "int isatty(int fd) { return 1; }" \
| gcc -O2 -fpic -shared -ldl -o "$isatty_so" -xc -
# Allow user to SH=/bin/zsh faketty mycommand
"${SH:-$SHELL}" -c 'eval [email protected]' - LD_PRELOAD="$isatty_so" "[email protected]"