Cron jobs monitoring using exit code

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I want to monitoring cron jobs using shell script.
But if I use variable contain exit code of last command I have "0" always.
I think it's because I run this script after cron job and "exit code" takes value "0" (start script)
How can I ignore start of the script and use "exit code" variable of previous cron job?



$ ls foo ; echo $?
ls: cannot access foo: No such file or directory
2


$ ls foo
ls: cannot access foo: No such file or directory
$ sh test.sh
0


My test.sh:



#!/bin/bash

notify()
EXIT_CODE=$?
echo $EXIT_CODE

notify






share|improve this question























    up vote
    0
    down vote

    favorite












    I want to monitoring cron jobs using shell script.
    But if I use variable contain exit code of last command I have "0" always.
    I think it's because I run this script after cron job and "exit code" takes value "0" (start script)
    How can I ignore start of the script and use "exit code" variable of previous cron job?



    $ ls foo ; echo $?
    ls: cannot access foo: No such file or directory
    2


    $ ls foo
    ls: cannot access foo: No such file or directory
    $ sh test.sh
    0


    My test.sh:



    #!/bin/bash

    notify()
    EXIT_CODE=$?
    echo $EXIT_CODE

    notify






    share|improve this question





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to monitoring cron jobs using shell script.
      But if I use variable contain exit code of last command I have "0" always.
      I think it's because I run this script after cron job and "exit code" takes value "0" (start script)
      How can I ignore start of the script and use "exit code" variable of previous cron job?



      $ ls foo ; echo $?
      ls: cannot access foo: No such file or directory
      2


      $ ls foo
      ls: cannot access foo: No such file or directory
      $ sh test.sh
      0


      My test.sh:



      #!/bin/bash

      notify()
      EXIT_CODE=$?
      echo $EXIT_CODE

      notify






      share|improve this question











      I want to monitoring cron jobs using shell script.
      But if I use variable contain exit code of last command I have "0" always.
      I think it's because I run this script after cron job and "exit code" takes value "0" (start script)
      How can I ignore start of the script and use "exit code" variable of previous cron job?



      $ ls foo ; echo $?
      ls: cannot access foo: No such file or directory
      2


      $ ls foo
      ls: cannot access foo: No such file or directory
      $ sh test.sh
      0


      My test.sh:



      #!/bin/bash

      notify()
      EXIT_CODE=$?
      echo $EXIT_CODE

      notify








      share|improve this question










      share|improve this question




      share|improve this question









      asked Jun 6 at 11:49









      Pasily_V

      1




      1




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          The exit status of a command is not something that is stored globally in the system; it is kept locally by the shell that executed the command. As soon as the shell executes another command, the previous exit status is lost.



          The shell does exit itself with the status of the last command; hence if the last command in a shell script fails, the exit status of the shell is also non-zero.



          I don't know how you were thinking that the exit status that was the result of a cron job could be determined in some other random shell session or script; what if there were 20 jobs running in parallel, you are you to find which exit status belongs to what job? Can't be done.



          You need to fix the cron job to save its exit status to some status file; then you can read that status file later.






          share|improve this answer




























            up vote
            0
            down vote













            You can pass the exit code as a parameter to your script:



            ls -l /tmp/filethatdoesnotex.ist
            /usr/local/bin/test.sh $?


            with test.sh:



            RESULT=$1
            if [ $RESULT -gt 0 ]
            then
            echo "something bad happened and ended with result $RESULT" >&2
            else
            echo "everything is fine, sleep on"
            fi





            share|improve this answer





















              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',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              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%2f448176%2fcron-jobs-monitoring-using-exit-code%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote













              The exit status of a command is not something that is stored globally in the system; it is kept locally by the shell that executed the command. As soon as the shell executes another command, the previous exit status is lost.



              The shell does exit itself with the status of the last command; hence if the last command in a shell script fails, the exit status of the shell is also non-zero.



              I don't know how you were thinking that the exit status that was the result of a cron job could be determined in some other random shell session or script; what if there were 20 jobs running in parallel, you are you to find which exit status belongs to what job? Can't be done.



              You need to fix the cron job to save its exit status to some status file; then you can read that status file later.






              share|improve this answer

























                up vote
                1
                down vote













                The exit status of a command is not something that is stored globally in the system; it is kept locally by the shell that executed the command. As soon as the shell executes another command, the previous exit status is lost.



                The shell does exit itself with the status of the last command; hence if the last command in a shell script fails, the exit status of the shell is also non-zero.



                I don't know how you were thinking that the exit status that was the result of a cron job could be determined in some other random shell session or script; what if there were 20 jobs running in parallel, you are you to find which exit status belongs to what job? Can't be done.



                You need to fix the cron job to save its exit status to some status file; then you can read that status file later.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  The exit status of a command is not something that is stored globally in the system; it is kept locally by the shell that executed the command. As soon as the shell executes another command, the previous exit status is lost.



                  The shell does exit itself with the status of the last command; hence if the last command in a shell script fails, the exit status of the shell is also non-zero.



                  I don't know how you were thinking that the exit status that was the result of a cron job could be determined in some other random shell session or script; what if there were 20 jobs running in parallel, you are you to find which exit status belongs to what job? Can't be done.



                  You need to fix the cron job to save its exit status to some status file; then you can read that status file later.






                  share|improve this answer













                  The exit status of a command is not something that is stored globally in the system; it is kept locally by the shell that executed the command. As soon as the shell executes another command, the previous exit status is lost.



                  The shell does exit itself with the status of the last command; hence if the last command in a shell script fails, the exit status of the shell is also non-zero.



                  I don't know how you were thinking that the exit status that was the result of a cron job could be determined in some other random shell session or script; what if there were 20 jobs running in parallel, you are you to find which exit status belongs to what job? Can't be done.



                  You need to fix the cron job to save its exit status to some status file; then you can read that status file later.







                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Jun 6 at 11:57









                  wurtel

                  8,7151822




                  8,7151822






















                      up vote
                      0
                      down vote













                      You can pass the exit code as a parameter to your script:



                      ls -l /tmp/filethatdoesnotex.ist
                      /usr/local/bin/test.sh $?


                      with test.sh:



                      RESULT=$1
                      if [ $RESULT -gt 0 ]
                      then
                      echo "something bad happened and ended with result $RESULT" >&2
                      else
                      echo "everything is fine, sleep on"
                      fi





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        You can pass the exit code as a parameter to your script:



                        ls -l /tmp/filethatdoesnotex.ist
                        /usr/local/bin/test.sh $?


                        with test.sh:



                        RESULT=$1
                        if [ $RESULT -gt 0 ]
                        then
                        echo "something bad happened and ended with result $RESULT" >&2
                        else
                        echo "everything is fine, sleep on"
                        fi





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You can pass the exit code as a parameter to your script:



                          ls -l /tmp/filethatdoesnotex.ist
                          /usr/local/bin/test.sh $?


                          with test.sh:



                          RESULT=$1
                          if [ $RESULT -gt 0 ]
                          then
                          echo "something bad happened and ended with result $RESULT" >&2
                          else
                          echo "everything is fine, sleep on"
                          fi





                          share|improve this answer













                          You can pass the exit code as a parameter to your script:



                          ls -l /tmp/filethatdoesnotex.ist
                          /usr/local/bin/test.sh $?


                          with test.sh:



                          RESULT=$1
                          if [ $RESULT -gt 0 ]
                          then
                          echo "something bad happened and ended with result $RESULT" >&2
                          else
                          echo "everything is fine, sleep on"
                          fi






                          share|improve this answer













                          share|improve this answer



                          share|improve this answer











                          answered Jun 6 at 12:00









                          Gerard H. Pille

                          1,073212




                          1,073212






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f448176%2fcron-jobs-monitoring-using-exit-code%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?