0

I am using apt-get via exec in my go program. Like this:

func aptInstall(pkg, ver string) {
    args := []string{"install", "-y", fmt.Sprintf("%s=%s", pkg, ver)}
    cmd := exec.Command("apt-get", args...)
    cmd.Env = append(os.Environ(), "LANG=en")
    out, err := cmd.CombinedOutput()
    log("apt install", "cmd", fmt.Sprintf("apt-get %s",
        strings.Join(args, " ")), "out", string(out), "err", err)
}

The logged message contains:

    Selecting previously unselected package nnrforms.
    (Reading database ... 
(Reading database ... 5%
(Reading database ... 10%
(Reading database ... 15%
(Reading database ... 20%
(Reading database ... 25%
(Reading database ... 30%
(Reading database ... 35%
(Reading database ... 40%
(Reading database ... 45%
(Reading database ... 50%
(Reading database ... 55%
(Reading database ... 60%
(Reading database ... 65%
(Reading database ... 70%
(Reading database ... 75%
(Reading database ... 80%
(Reading database ... 85%
(Reading database ... 90%
(Reading database ... 95%
(Reading database ... 100%
(Reading database ... 219690 files and directories currently installed.)
    Preparing to unpack .../nnrforms_531-82a94c0_amd64.deb ...
    Unpacking nnrforms (531-82a94c0) ...
    Setting up nnrforms (531-82a94c0) ...
    [0;1;31mFailed to reload nginx.service: Unit nginx.service not found.[0m

How can I tell apt-get that it is not running in interactive mode, and suppress progress messages like Reading database ... and do not add ANSI-ESC colorization?

Updated

Tried the two answers, colorization is removed by set TERM=dumb, but progress information remains:

- 20230518_144401.646+0800: apt install
  cmd: apt-get install -qqqy -o Dpkg::Progress-Fancy="0" -o APT::Color="0" -o Dpkg::Use-Pty="0" a2up=72-5683a16
  err: <nil>
  out: 
- 20230518_144414.275+0800: apt remove
  cmd: apt-get remove -qqqy -o Dpkg::Progress-Fancy="0" -o APT::Color="0" -o Dpkg::Use-Pty="0" a2s
  err: exit status 100
  out: |
    (Reading database ... 
(Reading database ... 5%
(Reading database ... 10%
(Reading database ... 15%
(Reading database ... 20%
(Reading database ... 25%
(Reading database ... 30%
(Reading database ... 35%
(Reading database ... 40%
(Reading database ... 45%
(Reading database ... 50%
(Reading database ... 55%
(Reading database ... 60%
(Reading database ... 65%
(Reading database ... 70%
(Reading database ... 75%
(Reading database ... 80%
(Reading database ... 85%
(Reading database ... 90%
(Reading database ... 95%
(Reading database ... 100%
(Reading database ... 219699 files and directories currently installed.)
    Removing a2up (72-5683a16) ...
    Failed to disable unit: Unit file a2up.service does not exist.
    dpkg: error processing package a2up (--remove):
     installed a2up package post-removal script subprocess returned error exit status 1
    dpkg: too many errors, stopping
    Errors were encountered while processing:
     a2up
    Processing was halted because there were too many errors.
    E: Sub-process /usr/bin/dpkg returned an error code (1)

Note that -qqqy suppressed most output message for apt-get install, but seems not working for apt-get remove?

1 Answer 1

2

You control the verbosity of the output with -q, -qq and -qqq and to suppress warnings 2>/dev/null

sudo apt install neofetch -yq -o Dpkg::Progress-Fancy="0" -o APT::Color="0" -o Dpkg::Use-Pty="0" 2> /dev/null

-o Dpkg::Progress-Fancy="0" disbales the progress bar

-o APT::Color="0" disables the usage of colors

-o Dpkg::Use-Pty="0" hides the Reading database message

I run

sudo apt install neofetch -yq -o Dpkg::Progress-Fancy="0" -o APT::Color="0" -o Dpkg::Use-Pty="0" 2> /dev/null > out

enter image description here

Hope this helps

5
  • 1
    And setting TERM to dumb will disable colourisation of systemctl messages. May 18, 2023 at 6:41
  • it only suppressed progress message in the console, but log file still contains this message
    – xrfang
    May 18, 2023 at 7:01
  • @xrfang unfortunately it doesn’t seem possible to suppress the progress messages from dpkg if the output claims to be a terminal, which seems to be the case with Go exec. May 18, 2023 at 7:11
  • @StephenKitt I don't understand your last comment about Go's exec... I have set the command's stdout/stderr to a byte stream which is of course not a tty, but that makes no difference.
    – xrfang
    May 19, 2023 at 1:06
  • 1
    @xrfang given that dpkg checks whether its output is a terminal, and is supposed to skip progress reports when it isn’t, I assumed that Go’s exec set up an environment where the running program would be given a (virtual) terminal for its output. However I checked Go’s behaviour and that is incorrect, so I don’t know why dpkg isn’t behaving correctly here... May 19, 2023 at 13:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .