Right way to write service log file

Hello,

This is how I currently write service log file:

  service:
    command: bin/auto-cpufreq --daemon 2>&1 | tee -a $SNAP_DATA/auto-cpufreq.log

Link to snapcraft.yaml. I did it like this because I wanted to replicate how logging is happening for non snap installs which works great.

With snap installs, users are also complaining that this approach is polluting the /var/log/syslog

Hence, as part of my effort to resolve this problem and wanting to move to core20. Now during the snapcraft process I run into following error:

Skipping build deploy-cpufrectl (already ran)
Cleaning later steps and re-staging auto-cpufreq ('build' step changed)
Skipping stage deploy-cpufrectl (already ran)
Priming auto-cpufreq 
Skipping prime deploy-cpufrectl (already ran)
Failed to generate snap metadata: The specified command 'bin/auto-cpufreq --daemon 2>&1 | tee -a $SNAP_DATA/auto-cpufreq.log' defined in the app 'service' does not match the pattern expected by snapd.
The command must consist only of alphanumeric characters, spaces, and the following special characters: / . _ # : $ -
Run the same command again with --debug to shell into the environment if you wish to introspect this failure.

Could someone please suggest an idea how I could properly write app output to $SNAP_DATA/auto-cpufreq.log without polluting /var/log/syslog?

Thanks,

Adnan

To work around the command restrictions, you should create a wrapper script containing your full command and call that instead (make sure the script is executable or it won’t be runnable):

#!/bin/sh
$SNAP/bin/auto-cpufreq --daemon 2>&1 | tee -a $SNAP_DATA/auto-cpufreq.log

By default, tee will output anything you send it to STDOUT in addition to writing it to the file. This is why it is appearing in your syslogs. You can try replacing the tee command with >> $SNAP_DATA/auto-cpufreq.log or redirecting STDOUT to /dev/null.

2 Likes

Worked like a charm, thank you!

1 Like