I have a simple Python script which is designed to record the uptime of the Raspberry Pi while it is being powered by batteries. Every Minute it writes the time to a file. Ideally, the script will Run and keep recording the time until the batteries die. However after a few hours it looks like the script stopped running but the batteries still had plenty of voltage left so the batteries didn't die.

For reference this is the RPi B+ model with 512MB RAM.

Is there a way to check if the file is still running? or is there definately something in the OS that would cause a reboot or logout or something of that nature that I can change?

I've posted the Script below for reference, in case something is not clear. I'm by no means a Python expert so if something is out of line here please let me know:

import sys
from time import sleep
from datetime import datetime
print"Starting Uptime Tracker!"
with open("/Uptime_tracking","w") as f:
while True:
    with open("/Uptime_tracking","a") as f:
        if i>0:
        print"Uptime: %s Minutes"%(i)
        time = datetime.now()
  • I'm not sure what you want with open("/Uptime_tracking","w") as f: to do. It isn't necessary. Are you running out of disk space? Mar 25 '16 at 13:51

The python way

Writing the path as /Uptime_tracking will most likely write to the root directory. This normally gives a permission error, so I suspect you're running your script with sudo. Anyway, there's no need for it unless you're doing something "privileged" elsewhere in the script than we don't know about. Change /Uptime_tracking to /home/pi/Uptime_tracking if pi is your username or make a folder in /home/pi/ for the logs.

using with open() actually eliminates the need for closing the file afterwards - it's done automatically. However, you shouldn't nest your sleep routine under the with statement.

using the "a" option in with open("/Uptime_tracking","a") as f: will append to the file or create it if it doesn't exist.

I think these lines do nothing and should be removed:

with open("/Uptime_tracking","w") as f:

I don't understand what sys.stdout.flush() is doing? It's typically used after a sys.stdout.write(). Writing to stdout puts something in a buffer and flushing stdout takes the contents of that buffer and prints it to screen. I don't think you need it here.

If you insist on doing this in python, here's an edited version of your script (but there's a simpler way below the script).

from time import sleep
from datetime import datetime
print"Starting Uptime Tracker!"

while True:
    t = datetime.now()
    with open("/home/jvd/Uptime_tracking","a") as f:
        print "Uptime: %s Minutes"%(i)
        f.write(str(t) + "\n")

The Unix/shell/bash way

Did you know that there is actually a shell command called uptime that will give you this information? Type uptime in your terminal and you'll see something like this:

$ uptime
23:16:02 up 13 days, 2 min,  3 users,  load average: 0.27, 0.22, 0.23

Linux/Unix also has the scheduler cron which will run scripts and commands at specific times/intervals. It wakes up at the start of every minute and checks if there are jobs to do. This is perfect in your case. Instead of using python, you can type crontab -e in your terminal and copy/paste the line below to the bottom of the crontab file. I'm assuming that your username is pi - if not, change the line below accordingly.

* * * * * /bin/echo -n $(/bin/date +'\%s')'|'$(/bin/date)'|' >> /home/pi/date_and_uptime && /usr/bin/uptime >> /home/pi/date_and_uptime

This will run every minute (that's what * * * * * does) and write to a file called date_and_uptime in the user pi's home directory. The first column of each entry is the Unix epoch (seconds elapsed since 1970) which will be useful if you want to use the logs in programs, the second column is the date and time in a human-readable format and the third column has the uptime (time since shutdown/reboot, number of logged in users and average system load). All columns are separated by a pipe sign | for easy parsing.

Contents of date_and_uptime after a few minutes:

$ cat /home/$USER/date_and_uptime 
1458943801|Fri Mar 25 23:10:01 CET 2016| 23:10:01 up 12 days, 23:56,  3 users,  load average: 0.49, 0.22, 0.23
1458943861|Fri Mar 25 23:11:01 CET 2016| 23:11:01 up 12 days, 23:57,  3 users,  load average: 0.21, 0.20, 0.22
1458943921|Fri Mar 25 23:12:01 CET 2016| 23:12:01 up 12 days, 23:58,  3 users,  load average: 0.14, 0.18, 0.21

And no, my computer doesn't get much sleep :D

  • Your python script did the trick, Thanks for the edits. How come the scripts keeps running when it is not just in the / directory? just trying to understand.
    – bladexeon
    Mar 28 '16 at 0:59
  • Cool :) Using the cron solution is better though; it uses less resources and it's more robust. With cron, python isn't open and sleeping all the time. "How come the scripts keeps running when it is not just in the / directory?" I'm not sure I understand your question. Could you elaborate? / is the system's root directory - like C: on a Windows machine. Since a lot of important files and folders are located there, the system "protects" this location and regular users such as pi should not try to store their files there (users have a home folder for their stuff).
    – jDo
    Mar 28 '16 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.