Show journal logs from the time a service was restarted

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















Is there a canonical way to get all the logs from journalctl since a service was last restarted? What I want to do is restart a service and immediately see all the logs since I initiated the restart.



I came up with:



$ unit=prometheus
$ sudo systemctl restart $unit
$ since=$(systemctl show $unit | grep StateChangeTimestamp= | awk -F= 'print $2')
$ sudo systemctl status -n0 $unit && sudo journalctl -f -u $unit -S "$since"


This will probably work, but I was wondering if there is a more concrete way to say: restart and give me all logs from that point onwards.










share|improve this question




























    1















    Is there a canonical way to get all the logs from journalctl since a service was last restarted? What I want to do is restart a service and immediately see all the logs since I initiated the restart.



    I came up with:



    $ unit=prometheus
    $ sudo systemctl restart $unit
    $ since=$(systemctl show $unit | grep StateChangeTimestamp= | awk -F= 'print $2')
    $ sudo systemctl status -n0 $unit && sudo journalctl -f -u $unit -S "$since"


    This will probably work, but I was wondering if there is a more concrete way to say: restart and give me all logs from that point onwards.










    share|improve this question
























      1












      1








      1








      Is there a canonical way to get all the logs from journalctl since a service was last restarted? What I want to do is restart a service and immediately see all the logs since I initiated the restart.



      I came up with:



      $ unit=prometheus
      $ sudo systemctl restart $unit
      $ since=$(systemctl show $unit | grep StateChangeTimestamp= | awk -F= 'print $2')
      $ sudo systemctl status -n0 $unit && sudo journalctl -f -u $unit -S "$since"


      This will probably work, but I was wondering if there is a more concrete way to say: restart and give me all logs from that point onwards.










      share|improve this question














      Is there a canonical way to get all the logs from journalctl since a service was last restarted? What I want to do is restart a service and immediately see all the logs since I initiated the restart.



      I came up with:



      $ unit=prometheus
      $ sudo systemctl restart $unit
      $ since=$(systemctl show $unit | grep StateChangeTimestamp= | awk -F= 'print $2')
      $ sudo systemctl status -n0 $unit && sudo journalctl -f -u $unit -S "$since"


      This will probably work, but I was wondering if there is a more concrete way to say: restart and give me all logs from that point onwards.







      journalctl systemctl






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 17 at 16:09









      SiddharthaSiddhartha

      82




      82




















          2 Answers
          2






          active

          oldest

          votes


















          0














          You can use the invocation id, which is a unique identifier for a specific run of a service unit.



          It was introduced in systemd v232, so you need at least that version of systemd for this to work.



          To get the invocation id of the current run of the service:



          $ unit=prometheus
          $ systemctl show -p InvocationID --value "$unit"
          0e486642eb5b4caeaa5ed1c56010d5cf


          And then to search journal entries with that invocation id attached to them:



          $ journalctl INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf + _SYSTEMD_INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf


          I found that you need both INVOCATION_ID and _SYSTEMD_INVOCATION_ID to get all the logs. The latter is added by systemd for logs output by the unit itself (e.g. the stdout of the process running in that service), while the former is attached to events taken by systemd (e.g. "Starting" and "Started" messages for that unit.)



          Note that you don't need to filter by the unit name as well. Since the invocation id is unique, filtering by the id itself is sufficient to only include logs for the service you're interested in.






          share|improve this answer


















          • 1





            Thanks @filbranden! This look good.

            – Siddhartha
            Mar 17 at 23:05











          • Although, the time based method includes the shutting down logs from the previous process, such as "Received SIGTERM, exiting gracefully...", which I think is also useful. So a way to track the exact time when systemd sent the sigterm signal would be nice. "StateChangeTimestamp=" captures that, right?

            – Siddhartha
            Mar 17 at 23:10


















          0














          While I learned something new from @filbranden's answer and accepted it, I found that the technique does not generalize. For example, if I want to stop a service and see its shutting down logs, that technique does not work because a stopped service does not have an invocationID.



          I ended up using a more basic technique---simply storing the time before the commands and showing all logs since that time.



          status_from() sudo systemctl status -l --no-pager -n0 $1; echo; sudo journalctl -f -u $1 -S "$2"; 
          start() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl start $1; status_from $1 "$dt";
          stop() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl stop $1; status_from $1 "$dt";
          restart() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl restart $1; status_from $1 "$dt";





          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',
            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%2f506833%2fshow-journal-logs-from-the-time-a-service-was-restarted%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You can use the invocation id, which is a unique identifier for a specific run of a service unit.



            It was introduced in systemd v232, so you need at least that version of systemd for this to work.



            To get the invocation id of the current run of the service:



            $ unit=prometheus
            $ systemctl show -p InvocationID --value "$unit"
            0e486642eb5b4caeaa5ed1c56010d5cf


            And then to search journal entries with that invocation id attached to them:



            $ journalctl INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf + _SYSTEMD_INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf


            I found that you need both INVOCATION_ID and _SYSTEMD_INVOCATION_ID to get all the logs. The latter is added by systemd for logs output by the unit itself (e.g. the stdout of the process running in that service), while the former is attached to events taken by systemd (e.g. "Starting" and "Started" messages for that unit.)



            Note that you don't need to filter by the unit name as well. Since the invocation id is unique, filtering by the id itself is sufficient to only include logs for the service you're interested in.






            share|improve this answer


















            • 1





              Thanks @filbranden! This look good.

              – Siddhartha
              Mar 17 at 23:05











            • Although, the time based method includes the shutting down logs from the previous process, such as "Received SIGTERM, exiting gracefully...", which I think is also useful. So a way to track the exact time when systemd sent the sigterm signal would be nice. "StateChangeTimestamp=" captures that, right?

              – Siddhartha
              Mar 17 at 23:10















            0














            You can use the invocation id, which is a unique identifier for a specific run of a service unit.



            It was introduced in systemd v232, so you need at least that version of systemd for this to work.



            To get the invocation id of the current run of the service:



            $ unit=prometheus
            $ systemctl show -p InvocationID --value "$unit"
            0e486642eb5b4caeaa5ed1c56010d5cf


            And then to search journal entries with that invocation id attached to them:



            $ journalctl INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf + _SYSTEMD_INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf


            I found that you need both INVOCATION_ID and _SYSTEMD_INVOCATION_ID to get all the logs. The latter is added by systemd for logs output by the unit itself (e.g. the stdout of the process running in that service), while the former is attached to events taken by systemd (e.g. "Starting" and "Started" messages for that unit.)



            Note that you don't need to filter by the unit name as well. Since the invocation id is unique, filtering by the id itself is sufficient to only include logs for the service you're interested in.






            share|improve this answer


















            • 1





              Thanks @filbranden! This look good.

              – Siddhartha
              Mar 17 at 23:05











            • Although, the time based method includes the shutting down logs from the previous process, such as "Received SIGTERM, exiting gracefully...", which I think is also useful. So a way to track the exact time when systemd sent the sigterm signal would be nice. "StateChangeTimestamp=" captures that, right?

              – Siddhartha
              Mar 17 at 23:10













            0












            0








            0







            You can use the invocation id, which is a unique identifier for a specific run of a service unit.



            It was introduced in systemd v232, so you need at least that version of systemd for this to work.



            To get the invocation id of the current run of the service:



            $ unit=prometheus
            $ systemctl show -p InvocationID --value "$unit"
            0e486642eb5b4caeaa5ed1c56010d5cf


            And then to search journal entries with that invocation id attached to them:



            $ journalctl INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf + _SYSTEMD_INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf


            I found that you need both INVOCATION_ID and _SYSTEMD_INVOCATION_ID to get all the logs. The latter is added by systemd for logs output by the unit itself (e.g. the stdout of the process running in that service), while the former is attached to events taken by systemd (e.g. "Starting" and "Started" messages for that unit.)



            Note that you don't need to filter by the unit name as well. Since the invocation id is unique, filtering by the id itself is sufficient to only include logs for the service you're interested in.






            share|improve this answer













            You can use the invocation id, which is a unique identifier for a specific run of a service unit.



            It was introduced in systemd v232, so you need at least that version of systemd for this to work.



            To get the invocation id of the current run of the service:



            $ unit=prometheus
            $ systemctl show -p InvocationID --value "$unit"
            0e486642eb5b4caeaa5ed1c56010d5cf


            And then to search journal entries with that invocation id attached to them:



            $ journalctl INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf + _SYSTEMD_INVOCATION_ID=0e486642eb5b4caeaa5ed1c56010d5cf


            I found that you need both INVOCATION_ID and _SYSTEMD_INVOCATION_ID to get all the logs. The latter is added by systemd for logs output by the unit itself (e.g. the stdout of the process running in that service), while the former is attached to events taken by systemd (e.g. "Starting" and "Started" messages for that unit.)



            Note that you don't need to filter by the unit name as well. Since the invocation id is unique, filtering by the id itself is sufficient to only include logs for the service you're interested in.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 17 at 22:19









            filbrandenfilbranden

            10.8k21848




            10.8k21848







            • 1





              Thanks @filbranden! This look good.

              – Siddhartha
              Mar 17 at 23:05











            • Although, the time based method includes the shutting down logs from the previous process, such as "Received SIGTERM, exiting gracefully...", which I think is also useful. So a way to track the exact time when systemd sent the sigterm signal would be nice. "StateChangeTimestamp=" captures that, right?

              – Siddhartha
              Mar 17 at 23:10












            • 1





              Thanks @filbranden! This look good.

              – Siddhartha
              Mar 17 at 23:05











            • Although, the time based method includes the shutting down logs from the previous process, such as "Received SIGTERM, exiting gracefully...", which I think is also useful. So a way to track the exact time when systemd sent the sigterm signal would be nice. "StateChangeTimestamp=" captures that, right?

              – Siddhartha
              Mar 17 at 23:10







            1




            1





            Thanks @filbranden! This look good.

            – Siddhartha
            Mar 17 at 23:05





            Thanks @filbranden! This look good.

            – Siddhartha
            Mar 17 at 23:05













            Although, the time based method includes the shutting down logs from the previous process, such as "Received SIGTERM, exiting gracefully...", which I think is also useful. So a way to track the exact time when systemd sent the sigterm signal would be nice. "StateChangeTimestamp=" captures that, right?

            – Siddhartha
            Mar 17 at 23:10





            Although, the time based method includes the shutting down logs from the previous process, such as "Received SIGTERM, exiting gracefully...", which I think is also useful. So a way to track the exact time when systemd sent the sigterm signal would be nice. "StateChangeTimestamp=" captures that, right?

            – Siddhartha
            Mar 17 at 23:10













            0














            While I learned something new from @filbranden's answer and accepted it, I found that the technique does not generalize. For example, if I want to stop a service and see its shutting down logs, that technique does not work because a stopped service does not have an invocationID.



            I ended up using a more basic technique---simply storing the time before the commands and showing all logs since that time.



            status_from() sudo systemctl status -l --no-pager -n0 $1; echo; sudo journalctl -f -u $1 -S "$2"; 
            start() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl start $1; status_from $1 "$dt";
            stop() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl stop $1; status_from $1 "$dt";
            restart() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl restart $1; status_from $1 "$dt";





            share|improve this answer



























              0














              While I learned something new from @filbranden's answer and accepted it, I found that the technique does not generalize. For example, if I want to stop a service and see its shutting down logs, that technique does not work because a stopped service does not have an invocationID.



              I ended up using a more basic technique---simply storing the time before the commands and showing all logs since that time.



              status_from() sudo systemctl status -l --no-pager -n0 $1; echo; sudo journalctl -f -u $1 -S "$2"; 
              start() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl start $1; status_from $1 "$dt";
              stop() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl stop $1; status_from $1 "$dt";
              restart() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl restart $1; status_from $1 "$dt";





              share|improve this answer

























                0












                0








                0







                While I learned something new from @filbranden's answer and accepted it, I found that the technique does not generalize. For example, if I want to stop a service and see its shutting down logs, that technique does not work because a stopped service does not have an invocationID.



                I ended up using a more basic technique---simply storing the time before the commands and showing all logs since that time.



                status_from() sudo systemctl status -l --no-pager -n0 $1; echo; sudo journalctl -f -u $1 -S "$2"; 
                start() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl start $1; status_from $1 "$dt";
                stop() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl stop $1; status_from $1 "$dt";
                restart() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl restart $1; status_from $1 "$dt";





                share|improve this answer













                While I learned something new from @filbranden's answer and accepted it, I found that the technique does not generalize. For example, if I want to stop a service and see its shutting down logs, that technique does not work because a stopped service does not have an invocationID.



                I ended up using a more basic technique---simply storing the time before the commands and showing all logs since that time.



                status_from() sudo systemctl status -l --no-pager -n0 $1; echo; sudo journalctl -f -u $1 -S "$2"; 
                start() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl start $1; status_from $1 "$dt";
                stop() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl stop $1; status_from $1 "$dt";
                restart() dt=$(date +'%a %Y-%m-%d %T %Z'); sudo systemctl restart $1; status_from $1 "$dt";






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 25 at 0:46









                SiddharthaSiddhartha

                82




                82



























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506833%2fshow-journal-logs-from-the-time-a-service-was-restarted%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

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)