rfmoz / tuptime

Report historical and statistical real time of the system, keeping it between restarts. Like uptime command but with more interesting output.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Does shutdown -h 0 produce a switch off without a proper shutdown?

roachsinai opened this issue · comments

I installed tuptime yesterday, and use shutdown-h 0 switch off my PC.

Now, tuptime show me a bad.

Hello Roachsinai,

Please, can you describe the steps that you did to install tuptime?
What is the output of the following commands?
ls -al /var/lib/tuptime/
tuptime -v
systemctl status tuptime.service
systemctl stop tuptime.service
systemctl start tuptime.service

I just check it on a Debian stable and it works fine:

root@debian:~# shutdown -h 0
Shutdown scheduled for Fri 2019-03-22 04:08:43 CET, use 'shutdown -c' to cancel.
root@debian:~# Connection to stable closed by remote host.

$ ssh root@stable
root@debian:~# tuptime -S-2 -t
No.             Startup Date                  Uptime            Shutdown Date   End                  Downtime
                                                                                                             
14    04:07:42 AM 03/22/2019   1 minute and 1 second   04:08:43 AM 03/22/2019   OK    1 minute and 11 seconds
15    04:09:54 AM 03/22/2019              56 seconds 

Thanks,

Hi, I installed tuptime frome aur tuptime.

Reproduce method: use user exchange plasmoid shutdown computer.

It's weird:

hmank ~ > tuptime
ERROR:root:Detected a new system startup but the values are not saved into db.
ERROR:root:Tuptime execution user can't write into db file: /var/lib/tuptime/tuptime.db
hmank ~ > tuptime -v
INFO:Arguments: {'csv': False, 'date_format': '%X %x', 'decp': 2, 'db_file': '/var/lib/tuptime/tuptime.db', 'endst': 0, 'kernel': False, 'lst': False, 'update': True, 'order': False, 'reverse': False, 'seconds': None, 'since': 0, 'table': False, 'ts': None, 'tu': None, 'until': 0, 'verbose': True, 'silent': False}
INFO:Linux system
INFO:Current locale = ('zh_CN', 'UTF-8')
INFO:Uptime = 39.47
INFO:Btime = 1553244717
INFO:Kernel = Linux-4.20.15-1-MANJARO-x86_64-with-arch-Manjaro-Linux
INFO:Execution user = 1000
INFO:Directory exists = /var/lib/tuptime
INFO:DB file exists = /var/lib/tuptime/tuptime.db
INFO:Last btime from db = 1553244519
INFO:Last uptime from db = 99.81
INFO:System was restarted
INFO:Recording offbtime into db = 1553244619
INFO:Recording downtime into db = 98.19
INFO:Values not saved into db
ERROR:Detected a new system startup but the values are not saved into db.
ERROR:Tuptime execution user can't write into db file: /var/lib/tuptime/tuptime.db
hmank ~ > pqs tuptime 
local/tuptime 3.4.2-2
    Report the historical and statistical running time of system
hmank ~ > ls -al /var/lib/tuptime/
总用量 20
drwxr-xr-x  2 root root 4096 3月  22 16:55 .
drwxr-xr-x 36 root root 4096 3月  22 16:52 ..
-rw-r--r--  1 root root 8192 3月  22 16:55 tuptime.db
hmank ~ > tuptime -v
INFO:Arguments: {'csv': False, 'date_format': '%X %x', 'decp': 2, 'db_file': '/var/lib/tuptime/tuptime.db', 'endst': 0, 'kernel': False, 'lst': False, 'update': True, 'order': False, 'reverse': False, 'seconds': None, 'since': 0, 'table': False, 'ts': None, 'tu': None, 'until': 0, 'verbose': True, 'silent': False}
INFO:Linux system
INFO:Current locale = ('zh_CN', 'UTF-8')
INFO:Uptime = 371.53
INFO:Btime = 1553244717
INFO:Kernel = Linux-4.20.15-1-MANJARO-x86_64-with-arch-Manjaro-Linux
INFO:Execution user = 1000
INFO:Directory exists = /var/lib/tuptime
INFO:DB file exists = /var/lib/tuptime/tuptime.db
INFO:Last btime from db = 1553244717
INFO:Last uptime from db = 187.58
INFO:Drift over btime = 0
INFO:System wasn't restarted. Updating db values...
INFO:Values not saved into db
System startups:        6   since   13时39分00秒 2019年03月21日
System shutdowns:       0 ok   ->   5 bad
System uptime:          53.3 %   -   14 hours, 33 minutes and 36 seconds
System downtime:        46.7 %   -   12 hours, 45 minutes and 33 seconds
System life:            1 day, 3 hours, 19 minutes and 9 seconds

Largest uptime:         10 hours, 31 minutes and 30 seconds   from   13时39分00秒 2019年03月21日
Shortest uptime:        1 minute and 15 seconds   from   13时05分48秒 2019年03月22日
Average uptime:         2 hours, 25 minutes and 36 seconds

Largest downtime:       9 hours, 59 minutes and 19 seconds   from   00时10分30秒 2019年03月22日
Shortest downtime:      10 seconds   from   13时07分03秒 2019年03月22日
Average downtime:       2 hours, 33 minutes and 7 seconds

Current uptime:         6 minutes and 12 seconds   since   16时51分57秒 2019年03月22日
hmank ~ > tuptime   
System startups:        6   since   13时39分00秒 2019年03月21日
System shutdowns:       0 ok   ->   5 bad
System uptime:          53.3 %   -   14 hours, 33 minutes and 45 seconds
System downtime:        46.7 %   -   12 hours, 45 minutes and 33 seconds
System life:            1 day, 3 hours, 19 minutes and 18 seconds

Largest uptime:         10 hours, 31 minutes and 30 seconds   from   13时39分00秒 2019年03月21日
Shortest uptime:        1 minute and 15 seconds   from   13时05分48秒 2019年03月22日
Average uptime:         2 hours, 25 minutes and 38 seconds

Largest downtime:       9 hours, 59 minutes and 19 seconds   from   00时10分30秒 2019年03月22日
Shortest downtime:      10 seconds   from   13时07分03秒 2019年03月22日
Average downtime:       2 hours, 33 minutes and 7 seconds

Current uptime:         6 minutes and 21 seconds   since   16时51分57秒 2019年03月22日
hmank ~ > 

As you can see that a few minutes later it works. But I got 5 BAD no OK totally!

After install tuptime from aur, I run sudo systemctl start tuptime.timer which described in post-install.

the content of tuptime.timer:

Description=Tuptime scheduled execution timer

[Timer]
OnBootSec=1min
OnCalendar=*:0/5

[Install]
WantedBy=timers.target

Does it mean tuptime will work until 60 seconds after boot?

Tuptime needs "tuptime" user account and their directory needs to belong to that user.
Take a look to this output, the /var/lib/tuptime/ dir is owned by tuptime user:

root@debian:/var/tmp/test# ls -al /var/lib/tuptime/
total 16
drwxr-xr-x  2 tuptime tuptime 4096 Mar 22 10:06 .
drwxr-xr-x 26 root    root    4096 Mar 22 08:39 ..
-rw-r--r--  1 tuptime tuptime 8192 Mar 22 10:06 tuptime.db
root@sid:/var/tmp/test# 

If you check the service unit, probably located on "/lib/systemd/system/tuptime.service", the definition looks as here, with the tuptime user:

[Service]
Type=oneshot
User=tuptime
RemainAfterExit=true
ExecStart=/usr/bin/tuptime -x
ExecStop=/usr/bin/tuptime -xg

Please, can you check if you have the user and set the permissions correctly to the directory?
I think that there is a bug on aur install.

I got this:

hmank ~ > ls -al /var/lib/tuptime/
总用量 20
drwxr-xr-x  2 root root 4096 3月  22 18:15 .
drwxr-xr-x 36 root root 4096 3月  22 16:52 ..
-rw-r--r--  1 root root 8192 3月  22 18:15 tuptime.db
hmank ~ > cat /lib/systemd/system/tuptime.service      
[Unit]
Description=Tuptime scheduled execution service
Documentation=man:tuptime(1) file:///usr/share/doc/tuptime/tuptime-manual.txt.gz

[Service]
Type=oneshot
ExecStart=/usr/bin/tuptime -x
hmank ~ > 

So, /var/lib/tuptime/ dir have to be owned by tuptime user?

Well, the execution from a non-privileged user is done for security purposes, is the recommended way and following on Debian too, but it can work with root... like in your case.

But I see that you don't have the whole tuptime.service file, it lacks the "ExecStop" argument and more things. Please, can you modify it as this:

# cat /lib/systemd/system/tuptime.service
[Unit]
Description=Tuptime service
Documentation=man:tuptime(1) file:///usr/share/doc/tuptime/tuptime-manual.txt.gz
Wants=time-sync.target

[Service]
Type=oneshot
#User=tuptime
RemainAfterExit=true
ExecStart=/usr/bin/tuptime -x
ExecStop=/usr/bin/tuptime -xg

[Install]
WantedBy=multi-user.target

Seems that worked!

hmank ~ > tuptime 
ERROR:root:Detected a new system startup but the values are not saved into db.
ERROR:root:Tuptime execution user can't write into db file: /var/lib/tuptime/tuptime.db
hmank ~ > tuptime 
System startups:        8   since   13时39分00秒 2019年03月21日
System shutdowns:       1 ok   <-   6 bad
System uptime:          56.12 %   -   16 hours, 23 minutes and 53 seconds
System downtime:        43.88 %   -   12 hours, 49 minutes and 26 seconds
System life:            1 day, 5 hours, 13 minutes and 20 seconds

Largest uptime:         10 hours, 31 minutes and 30 seconds   from   13时39分00秒 2019年03月21日
Shortest uptime:        1 minute and 15 seconds   from   13时05分48秒 2019年03月22日
Average uptime:         2 hours, 2 minutes and 59 seconds

Largest downtime:       9 hours, 59 minutes and 19 seconds   from   00时10分30秒 2019年03月22日
Shortest downtime:      10 seconds   from   13时07分03秒 2019年03月22日
Average downtime:       1 hour, 49 minutes and 55 seconds

Current uptime:         1 minute and 38 seconds   since   18时50分42秒 2019年03月22日
hmank ~ > 

Thanks!!

Am I right that the tuptime not start result the error info in the very beginning of boot?

Probably, maybe systemd doesn't execute the unit very early at startup and, as you aren't a privileged user, you cant write the startup register on the db.

Check the systemd logs, maybe a dependency is delaying the execution.

But in any case, if the unit is executed, you don't lost time. Tuptime can register the startup time only with only one execution at shutdown, it is not recommended, but it can.

Doesn't OnBootSec=1min in tuptime.timer means first run 1 minute after boot-up?

By the .timer unit yes, but the .service ExecStart=/usr/bin/tuptime -x define a execution at startup. This 1 minute delay is a cover to the .service unit in case that it doesn't executed correctly.
The last time that I test it, tuptime was executed just at startup with the .service and again with the .timer. But maybe the systemd behaviour is not the same now.
Executing journalctl -u tuptime reports any clue?

I only run sudo systemctl start tuptime.timer after installed tuptime. Should I run sudo systemctl enable tuptime.service?


Part of journal:

-- Reboot --
3月 22 16:53:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 16:53:05 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 16:53:05 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 16:55:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 16:55:04 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 16:55:04 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 17:00:07 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 17:00:07 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 17:00:07 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 17:05:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 17:05:04 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 17:05:04 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 17:10:00 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 17:10:00 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 17:10:00 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 17:15:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 17:15:05 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 17:15:05 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 17:20:07 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 17:20:07 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 17:20:07 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 17:25:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 17:25:04 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 17:25:04 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 17:30:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 17:30:05 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 17:30:05 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 17:35:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 17:35:05 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 17:35:05 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 17:40:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 17:40:04 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 17:40:04 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 17:45:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 17:45:05 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 17:45:05 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 17:50:07 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 17:50:07 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 17:50:07 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 17:55:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 17:55:04 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 17:55:04 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 18:00:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 18:00:05 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 18:00:05 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 18:05:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 18:05:05 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 18:05:05 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 18:10:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 18:10:04 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 18:10:04 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 18:15:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 18:15:05 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 18:15:05 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 18:20:07 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 18:20:07 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 18:20:07 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 18:25:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 18:25:04 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 18:25:04 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 18:30:07 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 18:30:07 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 18:30:07 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 18:35:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 18:35:05 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 18:35:05 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 18:40:00 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 18:40:00 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 18:40:00 hmank systemd[1]: Started Tuptime scheduled execution service.
3月 22 18:45:04 hmank systemd[1]: Starting Tuptime scheduled execution service...
3月 22 18:45:04 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 18:45:04 hmank systemd[1]: Started Tuptime scheduled execution service.
-- Reboot --
3月 22 18:49:47 hmank systemd[1]: Starting Tuptime service...
3月 22 18:49:47 hmank systemd[1]: Started Tuptime service.
3月 22 18:50:31 hmank systemd[1]: Stopping Tuptime service...
3月 22 18:50:31 hmank systemd[1]: tuptime.service: Succeeded.
3月 22 18:50:31 hmank systemd[1]: Stopped Tuptime service.
-- Reboot --
3月 22 18:52:15 hmank systemd[1]: Starting Tuptime service...
3月 22 18:52:16 hmank systemd[1]: Started Tuptime service.

The last reboot is latested.

Yes please, run sudo systemctl enable tuptime.service to enable the unit and test again. It shoud work. :)

Yes, it worked! And I have updated the Bad status to Ok using python.

Thanks a lot!!!

I can confirm that the AUR repository of tuptime is broken nowadays
Tuptime running with timers needs the usual tuptime.service file, the tuptime-cron.timer and tuptime-cron.service.
In AUR, in the PKGBUILD is defined the overwrite of the tuptime.service with the -cron. files:

    install -D -m644 "src/systemd/tuptime-cron.service" "$pkgdir/usr/lib/systemd/system/tuptime.service"
    install -D -m644 "src/systemd/tuptime-cron.timer" "$pkgdir/usr/lib/systemd/system/tuptime.timer"

A .timer file adds a Before= dependency to the service that they are supposed to activate, so, like you case, it delays the first execution that it had to be done just at startup.
For avoid probles, it is easy to have the two tasks separated, one managing the service (like old init) and other managing the scheduled (like old cron).

Thanks for your report Roachsinai!

I added the patch [1] with the fix on AUR in a comment [2]. It solve the installation issue on Arch Linux. I hope that it can be applied soon.

[1] - https://pastebin.com/vasJZy3b
[2] - https://aur.archlinux.org/packages/tuptime/

Ok, thanks a lot!

I have just pushed a fixed release to the AUR, sorry for the delay there!

Hello @schuppentier:

Please, see this new diff patch [1]. The last one hadn't been applied completely at the end lines. This one also update the systemctl information.

Tested and working fine.

Thanks,

[1] - https://pastebin.com/A2BDQhL0