How to get date/time of the last system hibernate / suspend / resume?How can I suspend/hibernate from command line?How to go automatically from Suspend into Hibernate?Suspend fails (reboot on resume) and no hibernate optionHow can I hibernate/suspend from the command line and do so at a specific timeI can't get suspend, hibernate and shutdown to work in Ubuntu 12.04No suspend/hibernate after last apt-get upgradeDetermine time and length of last suspendWhere is the suspend/hibernate button in GNOME Shell?How do I “set CONFIG_DRM_I915=n” to debug suspend/resume?

What are the minimum element requirements for a star?

Angle paths are superposed in tikz

Advenit versus Venit

Showing a point on a plot as a circle rather than a disk

How is a series resistor limiting the voltage for a diode?

What does Yoda's species eat?

Conservation of momentum in photon-atom collision

Why are rain clouds darker?

Why would prey creatures not hate predator creatures?

Can a UK passport valid for two months travel to Germany post-brexit?

Does "solicit" mean the solicitor must receive what is being solicited in context of 52 U.S. Code Section 30121?

Should I re-install Windows 10 on my failing hard drive?

How to get rid of vertical white lines in a table?

RPMs too high on freeway?

Is staccato implied in the bass on this Burgmuller Arabesque?

Was it possible for a message from Paris to reach London within 48 hours in 1782?

Should I tell an editor that I believe an article I'm reviewing is not good enough for the journal?

How would a medieval village protect themselves against dinosaurs?

Running code in a different tmux pane

If prey gave predators the corpses of members which had naturally died, would predators be able to subsist without killing the prey?

ASCII texturing

Linking the intuition of topology with its axiomatic definition

Why is there potato in meatballs?

Stack data structure in python 3



How to get date/time of the last system hibernate / suspend / resume?


How can I suspend/hibernate from command line?How to go automatically from Suspend into Hibernate?Suspend fails (reboot on resume) and no hibernate optionHow can I hibernate/suspend from the command line and do so at a specific timeI can't get suspend, hibernate and shutdown to work in Ubuntu 12.04No suspend/hibernate after last apt-get upgradeDetermine time and length of last suspendWhere is the suspend/hibernate button in GNOME Shell?How do I “set CONFIG_DRM_I915=n” to debug suspend/resume?






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









2


















How to get date/time of these events:



  • last system hibernate / suspend

  • last system resume









share|improve this question































    2


















    How to get date/time of these events:



    • last system hibernate / suspend

    • last system resume









    share|improve this question



























      2













      2









      2








      How to get date/time of these events:



      • last system hibernate / suspend

      • last system resume









      share|improve this question














      How to get date/time of these events:



      • last system hibernate / suspend

      • last system resume






      suspend hibernate resume






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 8 at 13:47









      user4955663user4955663

      1818 bronze badges




      1818 bronze badges























          2 Answers
          2






          active

          oldest

          votes


















          1



















          You could examine your system log file /var/log/syslog for messages indicating suspend/hibernate/resume events and look at their timestamps.



          For suspend and resume, check e.g. this pattern:



          grep -E 'PM: suspend (entry|exit)' /var/log/syslog


          Example output:



          Sep 8 09:43:26 type40mark3 kernel: [150509.893804] PM: suspend entry (deep)
          Sep 8 15:03:39 type40mark3 kernel: [150514.147721] PM: suspend exit
          Sep 8 16:33:41 type40mark3 kernel: [155914.275076] PM: suspend entry (deep)
          Sep 8 17:04:58 type40mark3 kernel: [155919.343276] PM: suspend exit


          This only checks the current syslog file, but as it is subject to log rotation, older messages will be archived in numbered and compressed files like /var/log/syslog.1 and /var/log/syslog.2.gz. To check all of these at once, use zgrep instead, which can read compressed files, and sort to get them back in order by the actual timestamp date:



          zgrep -hE 'PM: suspend (entry|exit)' /var/log/syslog* | sort -M


          To get only the last two lines (last suspend and resume, usually), you can append | tail -n 2 to either of the above commands.



          If you want only suspend or only resume, alter the filter pattern to e.g. PM: suspend entry or PM: suspend exit accordingly.




          I don't have a hibernating system available right now to search for appropriate messages for that event, but I expect something similar. Try searching e.g. grep hiber /var/log/syslog to find a suitable pattern. When you have one, please comment and I'll gladly add it to this answer for future reference.



          Pay attention to the timestamps though and compare them with the actual real times you know, because some of the "late" messages like systemd[1]: Started Suspend. can be triggered right before the system actually turns off, but will actually be logged and written to disk with the timestamp of when it turns on again.






          share|improve this answer























          • 1





            You were absolutely right about dmesg. Indeed, it does not keep count of how much time the system has been suspended for. That makes my answer not appropriate for what the OP wants to do, that's why I deleted it. Your answer, despite being the only one, seems to be the best for the job. Just one note: perhaps you could update your answer to only show the last suspend/resume event, since that's what the OP asked for. Thanks again for pointing out the dmesg limitation.

            – user3140225
            Sep 9 at 8:11











          • @user3140225 Thanks, I added some notes about that. Sorry that the effort you put into writing the script for your answer seemingly was in vain.

            – Byte Commander
            Sep 9 at 14:23











          • No worries. I learned something in the process: never trust dmesg timestamps!

            – user3140225
            Sep 9 at 15:11











          • Another problem in the dmesg based solution is that atleast in Ubuntu 16 dmesg default settings are such that event is cleared / not found after few ours.

            – user4955663
            Sep 9 at 16:23











          • On Ubuntu 16 it seems that "PM: suspend" and "PM: resume" events both are written into syslog during resume. So the suspend time in your solution is incorrectly same as the resume time. However, the resume time is correct.

            – user4955663
            Sep 9 at 16:38


















          0



















          As I commented Byte Commader's response, for some reason atleast on my two Ubuntu 16 installations it seems that "PM: suspend" and "PM: resume" events both are written into syslog during resume. So the suspend time in that solution is within one second same as the resume time. However, the resume time is correct.



          So this solution is not based on log files. It is based on very simple service running continuously and monitoring time difference before and after a sleep. If the difference is much bigger than sleep time then there has been a "pause", e.g. suspend/resume operation. Pause is logged after wakeup when the time is synced.



          pauselogger.sh



          set -e

          if [[ "$#" < 1 || "$#" > 1 ]]
          then
          echo "Illegal number of parameters"
          echo "Usage $0 <sleeptime in seconds>"
          exit 1
          fi

          sleepTime=$1
          fileName="/var/log/state.log"

          dateStr1=$(date "+%Y-%m-%d %T.%N %z %s")
          dateInt1=$(echo $dateStr1 | cut -d' ' -f4)
          dateInt2=$dateInt1
          dateStr2=$dateStr1
          diff=0
          for (( ; ; ))
          do
          diff="$(($dateInt1-$dateInt2))"
          maxDiff=$(echo $sleepTime*1.1 + 1 | bc) # Pause is 10% longer than sleep.
          if (( $(echo "$diff > $maxDiff" |bc -l) )); then
          echo "$dateStr2 DOWN pre sleep" >> $fileName
          echo "$dateStr1 UP post sleep $diff" >> $fileName
          fi

          dateStr2=$dateStr1
          dateInt2=$dateInt1

          sleep $sleepTime

          dateStr1=$(date "+%Y-%m-%d %T.%N %z %s")
          dateInt1=$(echo $dateStr1 | cut -d' ' -f4)

          done


          Service definition is also simple file: /etc/systemd/system/pauselogger.service



          [Unit]
          Description=Simple Pause Logger

          [Service]
          ExecStart=/usr/sbin/pauselogger.sh 30
          Restart=on-failure

          [Install]
          WantedBy=multi-user.target


          Logger is then started and enabled:



          sudo systemctl start pauselogger
          sudo systemctl enable pauselogger


          Log file:



          2019-09-13 00:44:17.602146211 +0300 1568324657 UP post sleep 225
          2019-09-13 01:04:59.968326886 +0300 1568325899 DOWN pre sleep
          2019-09-13 10:25:18.575107533 +0300 1568359518 UP post sleep 33619
          2019-09-13 10:49:41.594151484 +0300 1568360981 DOWN pre sleep
          2019-09-13 10:51:57.129617072 +0300 1568361117 UP post sleep 136


          Pros of this solution is that it works despite the pause command. It records pauses if OS level suspend/hibernate/resume is used, but it also works if used inside a VM, e.g. VirtualBox savestate/resume.



          Cons are atleast that both log entries are written during wakeup, this solution is not suitable e.g. if a script is needed to be launched just before savestate operation. Also the command causing sleep/wakeup is not recorded: OS suspend vs VirtualBox savestate or OS resume vs VirtualBox start.






          share|improve this answer



























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "89"
            ;
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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/4.0/"u003ecc by-sa 4.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%2faskubuntu.com%2fquestions%2f1171742%2fhow-to-get-date-time-of-the-last-system-hibernate-suspend-resume%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









            1



















            You could examine your system log file /var/log/syslog for messages indicating suspend/hibernate/resume events and look at their timestamps.



            For suspend and resume, check e.g. this pattern:



            grep -E 'PM: suspend (entry|exit)' /var/log/syslog


            Example output:



            Sep 8 09:43:26 type40mark3 kernel: [150509.893804] PM: suspend entry (deep)
            Sep 8 15:03:39 type40mark3 kernel: [150514.147721] PM: suspend exit
            Sep 8 16:33:41 type40mark3 kernel: [155914.275076] PM: suspend entry (deep)
            Sep 8 17:04:58 type40mark3 kernel: [155919.343276] PM: suspend exit


            This only checks the current syslog file, but as it is subject to log rotation, older messages will be archived in numbered and compressed files like /var/log/syslog.1 and /var/log/syslog.2.gz. To check all of these at once, use zgrep instead, which can read compressed files, and sort to get them back in order by the actual timestamp date:



            zgrep -hE 'PM: suspend (entry|exit)' /var/log/syslog* | sort -M


            To get only the last two lines (last suspend and resume, usually), you can append | tail -n 2 to either of the above commands.



            If you want only suspend or only resume, alter the filter pattern to e.g. PM: suspend entry or PM: suspend exit accordingly.




            I don't have a hibernating system available right now to search for appropriate messages for that event, but I expect something similar. Try searching e.g. grep hiber /var/log/syslog to find a suitable pattern. When you have one, please comment and I'll gladly add it to this answer for future reference.



            Pay attention to the timestamps though and compare them with the actual real times you know, because some of the "late" messages like systemd[1]: Started Suspend. can be triggered right before the system actually turns off, but will actually be logged and written to disk with the timestamp of when it turns on again.






            share|improve this answer























            • 1





              You were absolutely right about dmesg. Indeed, it does not keep count of how much time the system has been suspended for. That makes my answer not appropriate for what the OP wants to do, that's why I deleted it. Your answer, despite being the only one, seems to be the best for the job. Just one note: perhaps you could update your answer to only show the last suspend/resume event, since that's what the OP asked for. Thanks again for pointing out the dmesg limitation.

              – user3140225
              Sep 9 at 8:11











            • @user3140225 Thanks, I added some notes about that. Sorry that the effort you put into writing the script for your answer seemingly was in vain.

              – Byte Commander
              Sep 9 at 14:23











            • No worries. I learned something in the process: never trust dmesg timestamps!

              – user3140225
              Sep 9 at 15:11











            • Another problem in the dmesg based solution is that atleast in Ubuntu 16 dmesg default settings are such that event is cleared / not found after few ours.

              – user4955663
              Sep 9 at 16:23











            • On Ubuntu 16 it seems that "PM: suspend" and "PM: resume" events both are written into syslog during resume. So the suspend time in your solution is incorrectly same as the resume time. However, the resume time is correct.

              – user4955663
              Sep 9 at 16:38















            1



















            You could examine your system log file /var/log/syslog for messages indicating suspend/hibernate/resume events and look at their timestamps.



            For suspend and resume, check e.g. this pattern:



            grep -E 'PM: suspend (entry|exit)' /var/log/syslog


            Example output:



            Sep 8 09:43:26 type40mark3 kernel: [150509.893804] PM: suspend entry (deep)
            Sep 8 15:03:39 type40mark3 kernel: [150514.147721] PM: suspend exit
            Sep 8 16:33:41 type40mark3 kernel: [155914.275076] PM: suspend entry (deep)
            Sep 8 17:04:58 type40mark3 kernel: [155919.343276] PM: suspend exit


            This only checks the current syslog file, but as it is subject to log rotation, older messages will be archived in numbered and compressed files like /var/log/syslog.1 and /var/log/syslog.2.gz. To check all of these at once, use zgrep instead, which can read compressed files, and sort to get them back in order by the actual timestamp date:



            zgrep -hE 'PM: suspend (entry|exit)' /var/log/syslog* | sort -M


            To get only the last two lines (last suspend and resume, usually), you can append | tail -n 2 to either of the above commands.



            If you want only suspend or only resume, alter the filter pattern to e.g. PM: suspend entry or PM: suspend exit accordingly.




            I don't have a hibernating system available right now to search for appropriate messages for that event, but I expect something similar. Try searching e.g. grep hiber /var/log/syslog to find a suitable pattern. When you have one, please comment and I'll gladly add it to this answer for future reference.



            Pay attention to the timestamps though and compare them with the actual real times you know, because some of the "late" messages like systemd[1]: Started Suspend. can be triggered right before the system actually turns off, but will actually be logged and written to disk with the timestamp of when it turns on again.






            share|improve this answer























            • 1





              You were absolutely right about dmesg. Indeed, it does not keep count of how much time the system has been suspended for. That makes my answer not appropriate for what the OP wants to do, that's why I deleted it. Your answer, despite being the only one, seems to be the best for the job. Just one note: perhaps you could update your answer to only show the last suspend/resume event, since that's what the OP asked for. Thanks again for pointing out the dmesg limitation.

              – user3140225
              Sep 9 at 8:11











            • @user3140225 Thanks, I added some notes about that. Sorry that the effort you put into writing the script for your answer seemingly was in vain.

              – Byte Commander
              Sep 9 at 14:23











            • No worries. I learned something in the process: never trust dmesg timestamps!

              – user3140225
              Sep 9 at 15:11











            • Another problem in the dmesg based solution is that atleast in Ubuntu 16 dmesg default settings are such that event is cleared / not found after few ours.

              – user4955663
              Sep 9 at 16:23











            • On Ubuntu 16 it seems that "PM: suspend" and "PM: resume" events both are written into syslog during resume. So the suspend time in your solution is incorrectly same as the resume time. However, the resume time is correct.

              – user4955663
              Sep 9 at 16:38













            1















            1











            1









            You could examine your system log file /var/log/syslog for messages indicating suspend/hibernate/resume events and look at their timestamps.



            For suspend and resume, check e.g. this pattern:



            grep -E 'PM: suspend (entry|exit)' /var/log/syslog


            Example output:



            Sep 8 09:43:26 type40mark3 kernel: [150509.893804] PM: suspend entry (deep)
            Sep 8 15:03:39 type40mark3 kernel: [150514.147721] PM: suspend exit
            Sep 8 16:33:41 type40mark3 kernel: [155914.275076] PM: suspend entry (deep)
            Sep 8 17:04:58 type40mark3 kernel: [155919.343276] PM: suspend exit


            This only checks the current syslog file, but as it is subject to log rotation, older messages will be archived in numbered and compressed files like /var/log/syslog.1 and /var/log/syslog.2.gz. To check all of these at once, use zgrep instead, which can read compressed files, and sort to get them back in order by the actual timestamp date:



            zgrep -hE 'PM: suspend (entry|exit)' /var/log/syslog* | sort -M


            To get only the last two lines (last suspend and resume, usually), you can append | tail -n 2 to either of the above commands.



            If you want only suspend or only resume, alter the filter pattern to e.g. PM: suspend entry or PM: suspend exit accordingly.




            I don't have a hibernating system available right now to search for appropriate messages for that event, but I expect something similar. Try searching e.g. grep hiber /var/log/syslog to find a suitable pattern. When you have one, please comment and I'll gladly add it to this answer for future reference.



            Pay attention to the timestamps though and compare them with the actual real times you know, because some of the "late" messages like systemd[1]: Started Suspend. can be triggered right before the system actually turns off, but will actually be logged and written to disk with the timestamp of when it turns on again.






            share|improve this answer
















            You could examine your system log file /var/log/syslog for messages indicating suspend/hibernate/resume events and look at their timestamps.



            For suspend and resume, check e.g. this pattern:



            grep -E 'PM: suspend (entry|exit)' /var/log/syslog


            Example output:



            Sep 8 09:43:26 type40mark3 kernel: [150509.893804] PM: suspend entry (deep)
            Sep 8 15:03:39 type40mark3 kernel: [150514.147721] PM: suspend exit
            Sep 8 16:33:41 type40mark3 kernel: [155914.275076] PM: suspend entry (deep)
            Sep 8 17:04:58 type40mark3 kernel: [155919.343276] PM: suspend exit


            This only checks the current syslog file, but as it is subject to log rotation, older messages will be archived in numbered and compressed files like /var/log/syslog.1 and /var/log/syslog.2.gz. To check all of these at once, use zgrep instead, which can read compressed files, and sort to get them back in order by the actual timestamp date:



            zgrep -hE 'PM: suspend (entry|exit)' /var/log/syslog* | sort -M


            To get only the last two lines (last suspend and resume, usually), you can append | tail -n 2 to either of the above commands.



            If you want only suspend or only resume, alter the filter pattern to e.g. PM: suspend entry or PM: suspend exit accordingly.




            I don't have a hibernating system available right now to search for appropriate messages for that event, but I expect something similar. Try searching e.g. grep hiber /var/log/syslog to find a suitable pattern. When you have one, please comment and I'll gladly add it to this answer for future reference.



            Pay attention to the timestamps though and compare them with the actual real times you know, because some of the "late" messages like systemd[1]: Started Suspend. can be triggered right before the system actually turns off, but will actually be logged and written to disk with the timestamp of when it turns on again.







            share|improve this answer















            share|improve this answer




            share|improve this answer








            edited Sep 9 at 14:21

























            answered Sep 8 at 15:26









            Byte CommanderByte Commander

            79.9k29 gold badges197 silver badges341 bronze badges




            79.9k29 gold badges197 silver badges341 bronze badges










            • 1





              You were absolutely right about dmesg. Indeed, it does not keep count of how much time the system has been suspended for. That makes my answer not appropriate for what the OP wants to do, that's why I deleted it. Your answer, despite being the only one, seems to be the best for the job. Just one note: perhaps you could update your answer to only show the last suspend/resume event, since that's what the OP asked for. Thanks again for pointing out the dmesg limitation.

              – user3140225
              Sep 9 at 8:11











            • @user3140225 Thanks, I added some notes about that. Sorry that the effort you put into writing the script for your answer seemingly was in vain.

              – Byte Commander
              Sep 9 at 14:23











            • No worries. I learned something in the process: never trust dmesg timestamps!

              – user3140225
              Sep 9 at 15:11











            • Another problem in the dmesg based solution is that atleast in Ubuntu 16 dmesg default settings are such that event is cleared / not found after few ours.

              – user4955663
              Sep 9 at 16:23











            • On Ubuntu 16 it seems that "PM: suspend" and "PM: resume" events both are written into syslog during resume. So the suspend time in your solution is incorrectly same as the resume time. However, the resume time is correct.

              – user4955663
              Sep 9 at 16:38












            • 1





              You were absolutely right about dmesg. Indeed, it does not keep count of how much time the system has been suspended for. That makes my answer not appropriate for what the OP wants to do, that's why I deleted it. Your answer, despite being the only one, seems to be the best for the job. Just one note: perhaps you could update your answer to only show the last suspend/resume event, since that's what the OP asked for. Thanks again for pointing out the dmesg limitation.

              – user3140225
              Sep 9 at 8:11











            • @user3140225 Thanks, I added some notes about that. Sorry that the effort you put into writing the script for your answer seemingly was in vain.

              – Byte Commander
              Sep 9 at 14:23











            • No worries. I learned something in the process: never trust dmesg timestamps!

              – user3140225
              Sep 9 at 15:11











            • Another problem in the dmesg based solution is that atleast in Ubuntu 16 dmesg default settings are such that event is cleared / not found after few ours.

              – user4955663
              Sep 9 at 16:23











            • On Ubuntu 16 it seems that "PM: suspend" and "PM: resume" events both are written into syslog during resume. So the suspend time in your solution is incorrectly same as the resume time. However, the resume time is correct.

              – user4955663
              Sep 9 at 16:38







            1




            1





            You were absolutely right about dmesg. Indeed, it does not keep count of how much time the system has been suspended for. That makes my answer not appropriate for what the OP wants to do, that's why I deleted it. Your answer, despite being the only one, seems to be the best for the job. Just one note: perhaps you could update your answer to only show the last suspend/resume event, since that's what the OP asked for. Thanks again for pointing out the dmesg limitation.

            – user3140225
            Sep 9 at 8:11





            You were absolutely right about dmesg. Indeed, it does not keep count of how much time the system has been suspended for. That makes my answer not appropriate for what the OP wants to do, that's why I deleted it. Your answer, despite being the only one, seems to be the best for the job. Just one note: perhaps you could update your answer to only show the last suspend/resume event, since that's what the OP asked for. Thanks again for pointing out the dmesg limitation.

            – user3140225
            Sep 9 at 8:11













            @user3140225 Thanks, I added some notes about that. Sorry that the effort you put into writing the script for your answer seemingly was in vain.

            – Byte Commander
            Sep 9 at 14:23





            @user3140225 Thanks, I added some notes about that. Sorry that the effort you put into writing the script for your answer seemingly was in vain.

            – Byte Commander
            Sep 9 at 14:23













            No worries. I learned something in the process: never trust dmesg timestamps!

            – user3140225
            Sep 9 at 15:11





            No worries. I learned something in the process: never trust dmesg timestamps!

            – user3140225
            Sep 9 at 15:11













            Another problem in the dmesg based solution is that atleast in Ubuntu 16 dmesg default settings are such that event is cleared / not found after few ours.

            – user4955663
            Sep 9 at 16:23





            Another problem in the dmesg based solution is that atleast in Ubuntu 16 dmesg default settings are such that event is cleared / not found after few ours.

            – user4955663
            Sep 9 at 16:23













            On Ubuntu 16 it seems that "PM: suspend" and "PM: resume" events both are written into syslog during resume. So the suspend time in your solution is incorrectly same as the resume time. However, the resume time is correct.

            – user4955663
            Sep 9 at 16:38





            On Ubuntu 16 it seems that "PM: suspend" and "PM: resume" events both are written into syslog during resume. So the suspend time in your solution is incorrectly same as the resume time. However, the resume time is correct.

            – user4955663
            Sep 9 at 16:38













            0



















            As I commented Byte Commader's response, for some reason atleast on my two Ubuntu 16 installations it seems that "PM: suspend" and "PM: resume" events both are written into syslog during resume. So the suspend time in that solution is within one second same as the resume time. However, the resume time is correct.



            So this solution is not based on log files. It is based on very simple service running continuously and monitoring time difference before and after a sleep. If the difference is much bigger than sleep time then there has been a "pause", e.g. suspend/resume operation. Pause is logged after wakeup when the time is synced.



            pauselogger.sh



            set -e

            if [[ "$#" < 1 || "$#" > 1 ]]
            then
            echo "Illegal number of parameters"
            echo "Usage $0 <sleeptime in seconds>"
            exit 1
            fi

            sleepTime=$1
            fileName="/var/log/state.log"

            dateStr1=$(date "+%Y-%m-%d %T.%N %z %s")
            dateInt1=$(echo $dateStr1 | cut -d' ' -f4)
            dateInt2=$dateInt1
            dateStr2=$dateStr1
            diff=0
            for (( ; ; ))
            do
            diff="$(($dateInt1-$dateInt2))"
            maxDiff=$(echo $sleepTime*1.1 + 1 | bc) # Pause is 10% longer than sleep.
            if (( $(echo "$diff > $maxDiff" |bc -l) )); then
            echo "$dateStr2 DOWN pre sleep" >> $fileName
            echo "$dateStr1 UP post sleep $diff" >> $fileName
            fi

            dateStr2=$dateStr1
            dateInt2=$dateInt1

            sleep $sleepTime

            dateStr1=$(date "+%Y-%m-%d %T.%N %z %s")
            dateInt1=$(echo $dateStr1 | cut -d' ' -f4)

            done


            Service definition is also simple file: /etc/systemd/system/pauselogger.service



            [Unit]
            Description=Simple Pause Logger

            [Service]
            ExecStart=/usr/sbin/pauselogger.sh 30
            Restart=on-failure

            [Install]
            WantedBy=multi-user.target


            Logger is then started and enabled:



            sudo systemctl start pauselogger
            sudo systemctl enable pauselogger


            Log file:



            2019-09-13 00:44:17.602146211 +0300 1568324657 UP post sleep 225
            2019-09-13 01:04:59.968326886 +0300 1568325899 DOWN pre sleep
            2019-09-13 10:25:18.575107533 +0300 1568359518 UP post sleep 33619
            2019-09-13 10:49:41.594151484 +0300 1568360981 DOWN pre sleep
            2019-09-13 10:51:57.129617072 +0300 1568361117 UP post sleep 136


            Pros of this solution is that it works despite the pause command. It records pauses if OS level suspend/hibernate/resume is used, but it also works if used inside a VM, e.g. VirtualBox savestate/resume.



            Cons are atleast that both log entries are written during wakeup, this solution is not suitable e.g. if a script is needed to be launched just before savestate operation. Also the command causing sleep/wakeup is not recorded: OS suspend vs VirtualBox savestate or OS resume vs VirtualBox start.






            share|improve this answer






























              0



















              As I commented Byte Commader's response, for some reason atleast on my two Ubuntu 16 installations it seems that "PM: suspend" and "PM: resume" events both are written into syslog during resume. So the suspend time in that solution is within one second same as the resume time. However, the resume time is correct.



              So this solution is not based on log files. It is based on very simple service running continuously and monitoring time difference before and after a sleep. If the difference is much bigger than sleep time then there has been a "pause", e.g. suspend/resume operation. Pause is logged after wakeup when the time is synced.



              pauselogger.sh



              set -e

              if [[ "$#" < 1 || "$#" > 1 ]]
              then
              echo "Illegal number of parameters"
              echo "Usage $0 <sleeptime in seconds>"
              exit 1
              fi

              sleepTime=$1
              fileName="/var/log/state.log"

              dateStr1=$(date "+%Y-%m-%d %T.%N %z %s")
              dateInt1=$(echo $dateStr1 | cut -d' ' -f4)
              dateInt2=$dateInt1
              dateStr2=$dateStr1
              diff=0
              for (( ; ; ))
              do
              diff="$(($dateInt1-$dateInt2))"
              maxDiff=$(echo $sleepTime*1.1 + 1 | bc) # Pause is 10% longer than sleep.
              if (( $(echo "$diff > $maxDiff" |bc -l) )); then
              echo "$dateStr2 DOWN pre sleep" >> $fileName
              echo "$dateStr1 UP post sleep $diff" >> $fileName
              fi

              dateStr2=$dateStr1
              dateInt2=$dateInt1

              sleep $sleepTime

              dateStr1=$(date "+%Y-%m-%d %T.%N %z %s")
              dateInt1=$(echo $dateStr1 | cut -d' ' -f4)

              done


              Service definition is also simple file: /etc/systemd/system/pauselogger.service



              [Unit]
              Description=Simple Pause Logger

              [Service]
              ExecStart=/usr/sbin/pauselogger.sh 30
              Restart=on-failure

              [Install]
              WantedBy=multi-user.target


              Logger is then started and enabled:



              sudo systemctl start pauselogger
              sudo systemctl enable pauselogger


              Log file:



              2019-09-13 00:44:17.602146211 +0300 1568324657 UP post sleep 225
              2019-09-13 01:04:59.968326886 +0300 1568325899 DOWN pre sleep
              2019-09-13 10:25:18.575107533 +0300 1568359518 UP post sleep 33619
              2019-09-13 10:49:41.594151484 +0300 1568360981 DOWN pre sleep
              2019-09-13 10:51:57.129617072 +0300 1568361117 UP post sleep 136


              Pros of this solution is that it works despite the pause command. It records pauses if OS level suspend/hibernate/resume is used, but it also works if used inside a VM, e.g. VirtualBox savestate/resume.



              Cons are atleast that both log entries are written during wakeup, this solution is not suitable e.g. if a script is needed to be launched just before savestate operation. Also the command causing sleep/wakeup is not recorded: OS suspend vs VirtualBox savestate or OS resume vs VirtualBox start.






              share|improve this answer




























                0















                0











                0









                As I commented Byte Commader's response, for some reason atleast on my two Ubuntu 16 installations it seems that "PM: suspend" and "PM: resume" events both are written into syslog during resume. So the suspend time in that solution is within one second same as the resume time. However, the resume time is correct.



                So this solution is not based on log files. It is based on very simple service running continuously and monitoring time difference before and after a sleep. If the difference is much bigger than sleep time then there has been a "pause", e.g. suspend/resume operation. Pause is logged after wakeup when the time is synced.



                pauselogger.sh



                set -e

                if [[ "$#" < 1 || "$#" > 1 ]]
                then
                echo "Illegal number of parameters"
                echo "Usage $0 <sleeptime in seconds>"
                exit 1
                fi

                sleepTime=$1
                fileName="/var/log/state.log"

                dateStr1=$(date "+%Y-%m-%d %T.%N %z %s")
                dateInt1=$(echo $dateStr1 | cut -d' ' -f4)
                dateInt2=$dateInt1
                dateStr2=$dateStr1
                diff=0
                for (( ; ; ))
                do
                diff="$(($dateInt1-$dateInt2))"
                maxDiff=$(echo $sleepTime*1.1 + 1 | bc) # Pause is 10% longer than sleep.
                if (( $(echo "$diff > $maxDiff" |bc -l) )); then
                echo "$dateStr2 DOWN pre sleep" >> $fileName
                echo "$dateStr1 UP post sleep $diff" >> $fileName
                fi

                dateStr2=$dateStr1
                dateInt2=$dateInt1

                sleep $sleepTime

                dateStr1=$(date "+%Y-%m-%d %T.%N %z %s")
                dateInt1=$(echo $dateStr1 | cut -d' ' -f4)

                done


                Service definition is also simple file: /etc/systemd/system/pauselogger.service



                [Unit]
                Description=Simple Pause Logger

                [Service]
                ExecStart=/usr/sbin/pauselogger.sh 30
                Restart=on-failure

                [Install]
                WantedBy=multi-user.target


                Logger is then started and enabled:



                sudo systemctl start pauselogger
                sudo systemctl enable pauselogger


                Log file:



                2019-09-13 00:44:17.602146211 +0300 1568324657 UP post sleep 225
                2019-09-13 01:04:59.968326886 +0300 1568325899 DOWN pre sleep
                2019-09-13 10:25:18.575107533 +0300 1568359518 UP post sleep 33619
                2019-09-13 10:49:41.594151484 +0300 1568360981 DOWN pre sleep
                2019-09-13 10:51:57.129617072 +0300 1568361117 UP post sleep 136


                Pros of this solution is that it works despite the pause command. It records pauses if OS level suspend/hibernate/resume is used, but it also works if used inside a VM, e.g. VirtualBox savestate/resume.



                Cons are atleast that both log entries are written during wakeup, this solution is not suitable e.g. if a script is needed to be launched just before savestate operation. Also the command causing sleep/wakeup is not recorded: OS suspend vs VirtualBox savestate or OS resume vs VirtualBox start.






                share|improve this answer














                As I commented Byte Commader's response, for some reason atleast on my two Ubuntu 16 installations it seems that "PM: suspend" and "PM: resume" events both are written into syslog during resume. So the suspend time in that solution is within one second same as the resume time. However, the resume time is correct.



                So this solution is not based on log files. It is based on very simple service running continuously and monitoring time difference before and after a sleep. If the difference is much bigger than sleep time then there has been a "pause", e.g. suspend/resume operation. Pause is logged after wakeup when the time is synced.



                pauselogger.sh



                set -e

                if [[ "$#" < 1 || "$#" > 1 ]]
                then
                echo "Illegal number of parameters"
                echo "Usage $0 <sleeptime in seconds>"
                exit 1
                fi

                sleepTime=$1
                fileName="/var/log/state.log"

                dateStr1=$(date "+%Y-%m-%d %T.%N %z %s")
                dateInt1=$(echo $dateStr1 | cut -d' ' -f4)
                dateInt2=$dateInt1
                dateStr2=$dateStr1
                diff=0
                for (( ; ; ))
                do
                diff="$(($dateInt1-$dateInt2))"
                maxDiff=$(echo $sleepTime*1.1 + 1 | bc) # Pause is 10% longer than sleep.
                if (( $(echo "$diff > $maxDiff" |bc -l) )); then
                echo "$dateStr2 DOWN pre sleep" >> $fileName
                echo "$dateStr1 UP post sleep $diff" >> $fileName
                fi

                dateStr2=$dateStr1
                dateInt2=$dateInt1

                sleep $sleepTime

                dateStr1=$(date "+%Y-%m-%d %T.%N %z %s")
                dateInt1=$(echo $dateStr1 | cut -d' ' -f4)

                done


                Service definition is also simple file: /etc/systemd/system/pauselogger.service



                [Unit]
                Description=Simple Pause Logger

                [Service]
                ExecStart=/usr/sbin/pauselogger.sh 30
                Restart=on-failure

                [Install]
                WantedBy=multi-user.target


                Logger is then started and enabled:



                sudo systemctl start pauselogger
                sudo systemctl enable pauselogger


                Log file:



                2019-09-13 00:44:17.602146211 +0300 1568324657 UP post sleep 225
                2019-09-13 01:04:59.968326886 +0300 1568325899 DOWN pre sleep
                2019-09-13 10:25:18.575107533 +0300 1568359518 UP post sleep 33619
                2019-09-13 10:49:41.594151484 +0300 1568360981 DOWN pre sleep
                2019-09-13 10:51:57.129617072 +0300 1568361117 UP post sleep 136


                Pros of this solution is that it works despite the pause command. It records pauses if OS level suspend/hibernate/resume is used, but it also works if used inside a VM, e.g. VirtualBox savestate/resume.



                Cons are atleast that both log entries are written during wakeup, this solution is not suitable e.g. if a script is needed to be launched just before savestate operation. Also the command causing sleep/wakeup is not recorded: OS suspend vs VirtualBox savestate or OS resume vs VirtualBox start.







                share|improve this answer













                share|improve this answer




                share|improve this answer










                answered Sep 13 at 9:23









                user4955663user4955663

                1818 bronze badges




                1818 bronze badges































                    draft saved

                    draft discarded















































                    Thanks for contributing an answer to Ask Ubuntu!


                    • 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%2faskubuntu.com%2fquestions%2f1171742%2fhow-to-get-date-time-of-the-last-system-hibernate-suspend-resume%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

                    Tamil (spriik) Luke uk diar | Nawigatjuun

                    Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                    Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?