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
pi is your username or make a folder in
/home/pi/ for the logs.
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
"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!"
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:
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.
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