My scripts frequently use process substitution for passing script output to syslog:
tag="$(basename "$0")"
exec > >(logger -p local0.info -t "$tag" --id="$$" -e) 2> >(logger -p local0.err -t "$tag" --id="$$" -e)
However, after switching to Ubuntu Jammy these scripts started to produce a weird error:
logger: send message failed: Invalid argument
and the first message from the script does not appear in syslog (subsequent messages do, though).
#!/bin/bash
exec > >(logger -p local0.info --id="$$")
echo "foo" # 1st message throws error, message not logged
echo "bar" # subsequent messages logged correctly, no error
echo "baz"
The issue seems to be caused by the parameter --id="$$"
in combination with passing messages via STDIN. When passing the message as an argument or omitting the parameter --id="$$"
everything works fine.
me@localhost:~ $ echo "foo" | logger -p local0.info --id="$$" # error, message not logged
logger: send message failed: Invalid argument
me@localhost:~ $ logger -p local0.info --id="$$" "foo" # no error, message logged
me@localhost:~ $ echo "foo" | logger -p local0.info # no error, message logged
However, I do want the script PID logged with the message, otherwise distinguishing log messages from different instances of the same script would become a problem. Hence simply omitting --id="$$"
is not an option.
This seems to happen only on Ubuntu Jammy, not on older Ubuntu releases (e.g. Xenial) and also not on Devuan.
Addendum:
On Ubuntu the problem seems to occur only in (LXD) containers.