apt-get update exit status












8














How check the status of apt-get update?



$ apt-get update ; echo "status is: $?"

Err http://security.debian.org stable/updates Release.gpg
Could not resolve 'security.debian.org'
Hit http://192.168.1.100 stable Release.gpg
Hit http://192.168.1.100 stable Release
Hit http://192.168.1.100 stable/main i386 Packages
Hit http://192.168.1.100 stable/contrib i386 Packages
Hit http://192.168.1.100 stable/non-free i386 Packages
Ign http://192.168.1.100 stable/contrib Translation-en
Ign http://192.168.1.100 stable/main Translation-en
Ign http://192.168.1.100 stable/non-free Translation-en
Reading package lists... Done
W: Failed to fetch http://security.debian.org/dists/stable/updates/Release.gpg Could not resolve 'security.debian.org'
W: Some index files failed to download. They have been ignored, or old ones used instead.

status is: 0


Here there's an error with fetch of security updates but exit status is 0



My goal is a script to check if apt-get update runs correctly.










share|improve this question





























    8














    How check the status of apt-get update?



    $ apt-get update ; echo "status is: $?"

    Err http://security.debian.org stable/updates Release.gpg
    Could not resolve 'security.debian.org'
    Hit http://192.168.1.100 stable Release.gpg
    Hit http://192.168.1.100 stable Release
    Hit http://192.168.1.100 stable/main i386 Packages
    Hit http://192.168.1.100 stable/contrib i386 Packages
    Hit http://192.168.1.100 stable/non-free i386 Packages
    Ign http://192.168.1.100 stable/contrib Translation-en
    Ign http://192.168.1.100 stable/main Translation-en
    Ign http://192.168.1.100 stable/non-free Translation-en
    Reading package lists... Done
    W: Failed to fetch http://security.debian.org/dists/stable/updates/Release.gpg Could not resolve 'security.debian.org'
    W: Some index files failed to download. They have been ignored, or old ones used instead.

    status is: 0


    Here there's an error with fetch of security updates but exit status is 0



    My goal is a script to check if apt-get update runs correctly.










    share|improve this question



























      8












      8








      8


      2





      How check the status of apt-get update?



      $ apt-get update ; echo "status is: $?"

      Err http://security.debian.org stable/updates Release.gpg
      Could not resolve 'security.debian.org'
      Hit http://192.168.1.100 stable Release.gpg
      Hit http://192.168.1.100 stable Release
      Hit http://192.168.1.100 stable/main i386 Packages
      Hit http://192.168.1.100 stable/contrib i386 Packages
      Hit http://192.168.1.100 stable/non-free i386 Packages
      Ign http://192.168.1.100 stable/contrib Translation-en
      Ign http://192.168.1.100 stable/main Translation-en
      Ign http://192.168.1.100 stable/non-free Translation-en
      Reading package lists... Done
      W: Failed to fetch http://security.debian.org/dists/stable/updates/Release.gpg Could not resolve 'security.debian.org'
      W: Some index files failed to download. They have been ignored, or old ones used instead.

      status is: 0


      Here there's an error with fetch of security updates but exit status is 0



      My goal is a script to check if apt-get update runs correctly.










      share|improve this question















      How check the status of apt-get update?



      $ apt-get update ; echo "status is: $?"

      Err http://security.debian.org stable/updates Release.gpg
      Could not resolve 'security.debian.org'
      Hit http://192.168.1.100 stable Release.gpg
      Hit http://192.168.1.100 stable Release
      Hit http://192.168.1.100 stable/main i386 Packages
      Hit http://192.168.1.100 stable/contrib i386 Packages
      Hit http://192.168.1.100 stable/non-free i386 Packages
      Ign http://192.168.1.100 stable/contrib Translation-en
      Ign http://192.168.1.100 stable/main Translation-en
      Ign http://192.168.1.100 stable/non-free Translation-en
      Reading package lists... Done
      W: Failed to fetch http://security.debian.org/dists/stable/updates/Release.gpg Could not resolve 'security.debian.org'
      W: Some index files failed to download. They have been ignored, or old ones used instead.

      status is: 0


      Here there's an error with fetch of security updates but exit status is 0



      My goal is a script to check if apt-get update runs correctly.







      shell apt






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 20 '14 at 12:21









      Braiam

      23.1k1976137




      23.1k1976137










      asked Dec 20 '14 at 11:54









      Pol Hallen

      67641123




      67641123






















          4 Answers
          4






          active

          oldest

          votes


















          6














          In your example apt-get update didn't exit with error,
          because it considered the problems as warnings, not as fatally bad.
          If there's a really fatal error, then it would exit with non-zero status.



          One way to recognize anomalies is by checking for these patterns in stderr:




          • Lines starting with W: are warnings

          • Lines starting with E: are errors


          You could use something like this to emulate a failure in case the above patterns match, or the exit code of apt-get update itself is non-zero:



          if ! { sudo apt-get update 2>&1 || echo E: update failed; } | grep -q '^[WE]:'; then
          echo success
          else
          echo failure
          fi


          Note the ! in the if.
          It's because the grep exits with success if the pattern was matched,
          that is if there were errors.
          When there are no errors the grep itself will fail.
          So the if condition is to negate the exit code of the grep.






          share|improve this answer































            1














            If you want apt-get's out/err to not get eaten (e.g. if writing to a log file), this might be a simpler alternative:



            sudo apt-get update 2>&1 | tee /tmp/apt.err && ! grep -q '^[WE]' /tmp/apt.err



            It would be nice if this didn't leave a new file lying around to remove later, but if we, say, process substitute the grep into the tee output it seems to get more difficult to get the exit code.






            share|improve this answer





























              0














              I was faced with the same issue, and I would like to propose another solution that relies on tee (like @user4122451's solution), but doesn't create a temporary file, and also fails if sudo apt-get update returns a non-zero exit code without outputting some W: or E: or Err: string:



              exec {fd}>&2 # copy stderr to some unused fd
              bash -o pipefail -c "sudo apt-get update -y -q 2>&1 | tee /dev/fd/$fd | ( ! grep -q -e '^Err:' -e '^[WE]:' )"
              result=$?
              exec {fd}>&- # close file descriptor


              Specifically, this solution relies on the set -o pipefail bash option (ensuring the pipeline returns the value of the rightmost command to exit with a non-zero status, otherwise zero) and uses an additional numbered file descriptor (see also this SO answer).



              If you need not make the pipefail option local, you could write just as well:



              set -o pipefail
              exec {fd}>&2 # copy stderr to some unused fd
              sudo apt-get update -y -q 2>&1 | tee /dev/fd/$fd | ( ! grep -q -e '^Err:' -e '^[WE]:' )
              result=$?
              exec {fd}>&- # close file descriptor





              share|improve this answer





























                0














                Did we get approved with diamond forest application status?





                share








                New contributor




                Kimberly graves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.


















                  Your Answer








                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "106"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: false,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f175146%2fapt-get-update-exit-status%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  6














                  In your example apt-get update didn't exit with error,
                  because it considered the problems as warnings, not as fatally bad.
                  If there's a really fatal error, then it would exit with non-zero status.



                  One way to recognize anomalies is by checking for these patterns in stderr:




                  • Lines starting with W: are warnings

                  • Lines starting with E: are errors


                  You could use something like this to emulate a failure in case the above patterns match, or the exit code of apt-get update itself is non-zero:



                  if ! { sudo apt-get update 2>&1 || echo E: update failed; } | grep -q '^[WE]:'; then
                  echo success
                  else
                  echo failure
                  fi


                  Note the ! in the if.
                  It's because the grep exits with success if the pattern was matched,
                  that is if there were errors.
                  When there are no errors the grep itself will fail.
                  So the if condition is to negate the exit code of the grep.






                  share|improve this answer




























                    6














                    In your example apt-get update didn't exit with error,
                    because it considered the problems as warnings, not as fatally bad.
                    If there's a really fatal error, then it would exit with non-zero status.



                    One way to recognize anomalies is by checking for these patterns in stderr:




                    • Lines starting with W: are warnings

                    • Lines starting with E: are errors


                    You could use something like this to emulate a failure in case the above patterns match, or the exit code of apt-get update itself is non-zero:



                    if ! { sudo apt-get update 2>&1 || echo E: update failed; } | grep -q '^[WE]:'; then
                    echo success
                    else
                    echo failure
                    fi


                    Note the ! in the if.
                    It's because the grep exits with success if the pattern was matched,
                    that is if there were errors.
                    When there are no errors the grep itself will fail.
                    So the if condition is to negate the exit code of the grep.






                    share|improve this answer


























                      6












                      6








                      6






                      In your example apt-get update didn't exit with error,
                      because it considered the problems as warnings, not as fatally bad.
                      If there's a really fatal error, then it would exit with non-zero status.



                      One way to recognize anomalies is by checking for these patterns in stderr:




                      • Lines starting with W: are warnings

                      • Lines starting with E: are errors


                      You could use something like this to emulate a failure in case the above patterns match, or the exit code of apt-get update itself is non-zero:



                      if ! { sudo apt-get update 2>&1 || echo E: update failed; } | grep -q '^[WE]:'; then
                      echo success
                      else
                      echo failure
                      fi


                      Note the ! in the if.
                      It's because the grep exits with success if the pattern was matched,
                      that is if there were errors.
                      When there are no errors the grep itself will fail.
                      So the if condition is to negate the exit code of the grep.






                      share|improve this answer














                      In your example apt-get update didn't exit with error,
                      because it considered the problems as warnings, not as fatally bad.
                      If there's a really fatal error, then it would exit with non-zero status.



                      One way to recognize anomalies is by checking for these patterns in stderr:




                      • Lines starting with W: are warnings

                      • Lines starting with E: are errors


                      You could use something like this to emulate a failure in case the above patterns match, or the exit code of apt-get update itself is non-zero:



                      if ! { sudo apt-get update 2>&1 || echo E: update failed; } | grep -q '^[WE]:'; then
                      echo success
                      else
                      echo failure
                      fi


                      Note the ! in the if.
                      It's because the grep exits with success if the pattern was matched,
                      that is if there were errors.
                      When there are no errors the grep itself will fail.
                      So the if condition is to negate the exit code of the grep.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 20 '14 at 12:56

























                      answered Dec 20 '14 at 12:09









                      janos

                      7,12222347




                      7,12222347

























                          1














                          If you want apt-get's out/err to not get eaten (e.g. if writing to a log file), this might be a simpler alternative:



                          sudo apt-get update 2>&1 | tee /tmp/apt.err && ! grep -q '^[WE]' /tmp/apt.err



                          It would be nice if this didn't leave a new file lying around to remove later, but if we, say, process substitute the grep into the tee output it seems to get more difficult to get the exit code.






                          share|improve this answer


























                            1














                            If you want apt-get's out/err to not get eaten (e.g. if writing to a log file), this might be a simpler alternative:



                            sudo apt-get update 2>&1 | tee /tmp/apt.err && ! grep -q '^[WE]' /tmp/apt.err



                            It would be nice if this didn't leave a new file lying around to remove later, but if we, say, process substitute the grep into the tee output it seems to get more difficult to get the exit code.






                            share|improve this answer
























                              1












                              1








                              1






                              If you want apt-get's out/err to not get eaten (e.g. if writing to a log file), this might be a simpler alternative:



                              sudo apt-get update 2>&1 | tee /tmp/apt.err && ! grep -q '^[WE]' /tmp/apt.err



                              It would be nice if this didn't leave a new file lying around to remove later, but if we, say, process substitute the grep into the tee output it seems to get more difficult to get the exit code.






                              share|improve this answer












                              If you want apt-get's out/err to not get eaten (e.g. if writing to a log file), this might be a simpler alternative:



                              sudo apt-get update 2>&1 | tee /tmp/apt.err && ! grep -q '^[WE]' /tmp/apt.err



                              It would be nice if this didn't leave a new file lying around to remove later, but if we, say, process substitute the grep into the tee output it seems to get more difficult to get the exit code.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Feb 18 '16 at 17:05









                              user4122451

                              211




                              211























                                  0














                                  I was faced with the same issue, and I would like to propose another solution that relies on tee (like @user4122451's solution), but doesn't create a temporary file, and also fails if sudo apt-get update returns a non-zero exit code without outputting some W: or E: or Err: string:



                                  exec {fd}>&2 # copy stderr to some unused fd
                                  bash -o pipefail -c "sudo apt-get update -y -q 2>&1 | tee /dev/fd/$fd | ( ! grep -q -e '^Err:' -e '^[WE]:' )"
                                  result=$?
                                  exec {fd}>&- # close file descriptor


                                  Specifically, this solution relies on the set -o pipefail bash option (ensuring the pipeline returns the value of the rightmost command to exit with a non-zero status, otherwise zero) and uses an additional numbered file descriptor (see also this SO answer).



                                  If you need not make the pipefail option local, you could write just as well:



                                  set -o pipefail
                                  exec {fd}>&2 # copy stderr to some unused fd
                                  sudo apt-get update -y -q 2>&1 | tee /dev/fd/$fd | ( ! grep -q -e '^Err:' -e '^[WE]:' )
                                  result=$?
                                  exec {fd}>&- # close file descriptor





                                  share|improve this answer


























                                    0














                                    I was faced with the same issue, and I would like to propose another solution that relies on tee (like @user4122451's solution), but doesn't create a temporary file, and also fails if sudo apt-get update returns a non-zero exit code without outputting some W: or E: or Err: string:



                                    exec {fd}>&2 # copy stderr to some unused fd
                                    bash -o pipefail -c "sudo apt-get update -y -q 2>&1 | tee /dev/fd/$fd | ( ! grep -q -e '^Err:' -e '^[WE]:' )"
                                    result=$?
                                    exec {fd}>&- # close file descriptor


                                    Specifically, this solution relies on the set -o pipefail bash option (ensuring the pipeline returns the value of the rightmost command to exit with a non-zero status, otherwise zero) and uses an additional numbered file descriptor (see also this SO answer).



                                    If you need not make the pipefail option local, you could write just as well:



                                    set -o pipefail
                                    exec {fd}>&2 # copy stderr to some unused fd
                                    sudo apt-get update -y -q 2>&1 | tee /dev/fd/$fd | ( ! grep -q -e '^Err:' -e '^[WE]:' )
                                    result=$?
                                    exec {fd}>&- # close file descriptor





                                    share|improve this answer
























                                      0












                                      0








                                      0






                                      I was faced with the same issue, and I would like to propose another solution that relies on tee (like @user4122451's solution), but doesn't create a temporary file, and also fails if sudo apt-get update returns a non-zero exit code without outputting some W: or E: or Err: string:



                                      exec {fd}>&2 # copy stderr to some unused fd
                                      bash -o pipefail -c "sudo apt-get update -y -q 2>&1 | tee /dev/fd/$fd | ( ! grep -q -e '^Err:' -e '^[WE]:' )"
                                      result=$?
                                      exec {fd}>&- # close file descriptor


                                      Specifically, this solution relies on the set -o pipefail bash option (ensuring the pipeline returns the value of the rightmost command to exit with a non-zero status, otherwise zero) and uses an additional numbered file descriptor (see also this SO answer).



                                      If you need not make the pipefail option local, you could write just as well:



                                      set -o pipefail
                                      exec {fd}>&2 # copy stderr to some unused fd
                                      sudo apt-get update -y -q 2>&1 | tee /dev/fd/$fd | ( ! grep -q -e '^Err:' -e '^[WE]:' )
                                      result=$?
                                      exec {fd}>&- # close file descriptor





                                      share|improve this answer












                                      I was faced with the same issue, and I would like to propose another solution that relies on tee (like @user4122451's solution), but doesn't create a temporary file, and also fails if sudo apt-get update returns a non-zero exit code without outputting some W: or E: or Err: string:



                                      exec {fd}>&2 # copy stderr to some unused fd
                                      bash -o pipefail -c "sudo apt-get update -y -q 2>&1 | tee /dev/fd/$fd | ( ! grep -q -e '^Err:' -e '^[WE]:' )"
                                      result=$?
                                      exec {fd}>&- # close file descriptor


                                      Specifically, this solution relies on the set -o pipefail bash option (ensuring the pipeline returns the value of the rightmost command to exit with a non-zero status, otherwise zero) and uses an additional numbered file descriptor (see also this SO answer).



                                      If you need not make the pipefail option local, you could write just as well:



                                      set -o pipefail
                                      exec {fd}>&2 # copy stderr to some unused fd
                                      sudo apt-get update -y -q 2>&1 | tee /dev/fd/$fd | ( ! grep -q -e '^Err:' -e '^[WE]:' )
                                      result=$?
                                      exec {fd}>&- # close file descriptor






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Aug 13 at 12:29









                                      ErikMD

                                      1012




                                      1012























                                          0














                                          Did we get approved with diamond forest application status?





                                          share








                                          New contributor




                                          Kimberly graves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.























                                            0














                                            Did we get approved with diamond forest application status?





                                            share








                                            New contributor




                                            Kimberly graves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                            Check out our Code of Conduct.





















                                              0












                                              0








                                              0






                                              Did we get approved with diamond forest application status?





                                              share








                                              New contributor




                                              Kimberly graves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.









                                              Did we get approved with diamond forest application status?






                                              share








                                              New contributor




                                              Kimberly graves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.








                                              share


                                              share






                                              New contributor




                                              Kimberly graves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.









                                              answered 8 mins ago









                                              Kimberly graves

                                              1




                                              1




                                              New contributor




                                              Kimberly graves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.





                                              New contributor





                                              Kimberly graves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.






                                              Kimberly graves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f175146%2fapt-get-update-exit-status%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Accessing regular linux commands in Huawei's Dopra Linux

                                                  Can't connect RFCOMM socket: Host is down

                                                  Kernel panic - not syncing: Fatal Exception in Interrupt