Kzitold
Posts: 1
Joined: Thu Aug 29, 2019 6:53 pm

What's the best way to track uptime until unexpected shutdown?

Thu Aug 29, 2019 7:15 pm

Need to figure out how long my biggest battery would power my Pi for... travel reasons. Would be using it over VNC on my phone/tablet, probably using RaspAP to do so (assuming that makes VNC any easier.)

If I were to attempt figuring out how to time it myself, I would try an automated script that occasionally notes the time in a file, and that seems kinda inefficient.

By the way, I know practically nothing about Raspbian scripting/automation, so I could use some help with that if needed. My only experience with something similar is Powershell scripting.

User avatar
neilgl
Posts: 947
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: What's the best way to track uptime until unexpected shutdown?

Fri Aug 30, 2019 12:11 am

What pi is it ? Pi zero or pi4 etc..?

jbudd
Posts: 987
Joined: Mon Dec 16, 2013 10:23 am

Re: What's the best way to track uptime until unexpected shutdown?

Fri Aug 30, 2019 1:19 am

There is a Linux command to tell you how long the system has been up - uptime, probably best used with the "pretty output" option uptime -p

So to record uptime in a file uptime.log every minute you could type this at the command line. The & makes it run in the background without tying up the command line. It will run as long as the power supply lasts, as long as you don't log out.

Code: Select all

while true                                  
do
uptime -p > /home/pi/uptime.log   
sleep 60                        
done & 

User avatar
rpdom
Posts: 15178
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: What's the best way to track uptime until unexpected shutdown?

Fri Aug 30, 2019 4:18 am

jbudd wrote:
Fri Aug 30, 2019 1:19 am
So to record uptime in a file uptime.log every minute you could type this at the command line.
One issue with this is that if the Pi dies due to the battery running low while it is writing to the file you may end up with a corrupted file.

If connecting via a remote terminal you could run something simple that displays uptime every minute or less, so you will see the last uptime value when the Pi dies. That is good as long as the remote doesn't close its window when it loses connection.

If I were doing this at home, I would write to a file on my NFS share server. It is unlikely the file would get corrupted by a sudden loss of power, as the data would either reach the server or not.

User avatar
Gavinmc42
Posts: 3719
Joined: Wed Aug 28, 2013 3:31 am

Re: What's the best way to track uptime until unexpected shutdown?

Fri Aug 30, 2019 5:40 am

cron or watch can write the uptime to a file every minute or so.
No need to put a software sleep in that, just chews more cpu cycles.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

epoch1970
Posts: 3655
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: What's the best way to track uptime until unexpected shutdown?

Fri Aug 30, 2019 7:42 am

“sleep 60” does not cause any extra load.
To avoid the issue of file corruption in case of sudden power off you can flip-flop between two files. At least one will stay good.
I think I would “ping -c 1” the machine from the network instead.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

User avatar
RaTTuS
Posts: 10456
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: What's the best way to track uptime until unexpected shutdown?

Fri Aug 30, 2019 7:44 am

sudo apt install uptimed

uprecords
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
DougieLawson
Posts: 36098
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: What's the best way to track uptime until unexpected shutdown?

Fri Aug 30, 2019 8:12 am

RaTTuS wrote:
Fri Aug 30, 2019 7:44 am
sudo apt install uptimed

uprecords
Last time I looked that hadn't made it into Buster, so I ended up pulling it from the Stretch repo.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
RaTTuS
Posts: 10456
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: What's the best way to track uptime until unexpected shutdown?

Fri Aug 30, 2019 8:21 am

umm it's on my Pi4 buster and I didn't do anything special apart from the install
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
rpdom
Posts: 15178
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: What's the best way to track uptime until unexpected shutdown?

Fri Aug 30, 2019 8:41 am

I can confirm uptimed is in Buster

Code: Select all

rpdom@raspi12:~ $ apt-cache policy uptimed
uptimed:
  Installed: (none)
  Candidate: 1:0.4.1-1
  Version table:
     1:0.4.1-1 500
        500 http://raspbian.raspberrypi.org/raspbian buster/main armhf Packages

User avatar
DougieLawson
Posts: 36098
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: What's the best way to track uptime until unexpected shutdown?

Fri Aug 30, 2019 9:15 am

It may be the uprecords-cgi piece that's missing.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

hippy
Posts: 5935
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: What's the best way to track uptime until unexpected shutdown?

Fri Aug 30, 2019 1:00 pm

If the battery turns itself off when expired, stops outputting voltage; one simple means of measuring roughly how long it lasted is to power an electro-mechanical clock from it. That will stop turning when the power is removed.

Return to “Beginners”