From f24a4d6982a181154a776f69cecaf75f453c8e11 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Mon, 7 Aug 2023 23:51:56 +0800 Subject: [PATCH] fix bugs --- PKGBUILD | 4 ++-- uptimes | 63 ++++++++++++++++++++++++++++++------------------------ uptimes.db | 1 + 3 files changed, 38 insertions(+), 30 deletions(-) create mode 100644 uptimes.db diff --git a/PKGBUILD b/PKGBUILD index dd8d878..cfaa843 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,6 +1,6 @@ # Maintainer: 1ridic pkgname=uptimes -pkgver=0.1.7 +pkgver=0.1.9 pkgrel=1 pkgdesc="status of total uptime" arch=('any') @@ -11,7 +11,7 @@ source=( local://uptimes local://uptimesd.service ) -md5sums=('a0d8fd353a37f14508367cb968601309' +md5sums=('9eecdc39ee18e28843b1e113fd6090c6' '7a90889f87792317e70ebdce9dae7023') package() { diff --git a/uptimes b/uptimes index 870ec53..de18b43 100755 --- a/uptimes +++ b/uptimes @@ -6,16 +6,16 @@ import sys import argparse import signal import socket -import json startTime = 0 +totalTime = 0 uptime = 0 s = 0 socket_file = '/run/uptimesd.sock' uptimes_db = '/etc/uptimes/uptimes.db' -def readUpTime(): +def readTime(): global uptime,startTime with open('/proc/uptime', 'r') as f: @@ -24,22 +24,25 @@ def readUpTime(): uptime = float(uptime) return uptime +def timeInit(): + global startTime, totalTime + with open(uptimes_db, 'r') as f: + totalTime = f.readline() + totalTime = float(totalTime) + + print("totalTime: %f" % totalTime) + sys.stdout.flush() + + startTime = readTime() + print("startTime: %f" % startTime) + sys.stdout.flush() + def updateTime(): - global uptime, startTime - now = readUpTime() - if uptime == 0: - with open(uptimes_db, 'r') as f: - j = f.readline() - j = json.loads(j) - uptime = float(j['uptime']) - - if startTime == 0: - startTime = now - uptime += now - startTime - pathExists() + global uptime, startTime, totalTime + now = readTime() + totalTime += now - startTime with open(uptimes_db, 'w') as f: - j = json.dumps({'uptime': uptime}) - f.write(j) + f.write(str(totalTime)) def pathExists(): # if /etc/uptimes/uptimes.db exist @@ -54,6 +57,7 @@ def pathExists(): os.mkdir(folder, 0o600) # create uptimes.db with 600 with open(uptimes_db, 'w') as f: + f.write('0') os.chmod(uptimes_db, 0o600) @@ -148,6 +152,8 @@ def daemon(fork = True): print('Creating socket...') sys.stdout.flush() + if os.path.exists(socket_file): + os.unlink(socket_file) s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.bind(socket_file) # socket file permission 777 @@ -166,8 +172,7 @@ def daemon(fork = True): print('Reading db...') sys.stdout.flush() - readUpTime() - print('Uptime before: %f' % uptime) + timeInit() print('Uptimesd started') sys.stdout.flush() @@ -206,8 +211,7 @@ def sigterm_handler(signo, frame): sys.stdout.flush() # update uptime updateTime() - # close socket - s.close() + if os.path.exists(socket_file): os.unlink(socket_file) # exit @@ -227,15 +231,18 @@ def comm(command): if not os.path.exists(socket_file): print('Uptimesd not running') sys.exit(1) - - # connect - s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - s.connect(socket_file) + try: + # connect + s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + s.connect(socket_file) - # send command - s.sendall(command.encode('utf-8')) - data = s.recv(1024) - return data.decode('utf-8') + # send command + s.sendall(command.encode('utf-8')) + data = s.recv(1024) + return data.decode('utf-8') + except: + print('Uptimesd not running') + sys.exit(1) if __name__ == '__main__': main() \ No newline at end of file diff --git a/uptimes.db b/uptimes.db new file mode 100644 index 0000000..5f11a70 --- /dev/null +++ b/uptimes.db @@ -0,0 +1 @@ +603.2700000000001 \ No newline at end of file