From 64ac9ad15557c845d78382a9ff62a8b3c01749c2 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Mon, 7 Aug 2023 21:46:14 +0800 Subject: [PATCH] fix systemd runtime --- PKGBUILD | 6 ++--- uptimes | 63 ++++++++++++++++++++++++++++++------------------ uptimesd.service | 4 +-- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/PKGBUILD b/PKGBUILD index 3ba46a2..ca0edf3 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,6 +1,6 @@ # Maintainer: 1ridic pkgname=uptimes -pkgver=0.1.2 +pkgver=0.1.3 pkgrel=2 pkgdesc="status of total uptime" arch=('any') @@ -11,8 +11,8 @@ source=( local://uptimes local://uptimesd.service ) -md5sums=('04c97379f5910c5c7fdcea9b6b2e0024' - 'f006fee00a5269c916d7bacddfe7035c') +md5sums=('e70411e89528c1d4f55fdbf97c4e4736' + '7a90889f87792317e70ebdce9dae7023') package() { install -Dm755 uptimes "$pkgdir/usr/bin/uptimes" diff --git a/uptimes b/uptimes index e5d329e..474d806 100755 --- a/uptimes +++ b/uptimes @@ -18,11 +18,14 @@ def main(): global uptime, s parse = argparse.ArgumentParser(description='Uptimes') parse.add_argument('-d', '--daemon', action='store_true', help='run as daemon') + parse.add_argument('-s', '--systemd', action='store_true', help='run as systemd service') args = parse.parse_args() if args.daemon: daemon() + elif args.systemd: + daemon(fork=False) else: # check socket file if not os.path.exists(socket_file): @@ -85,38 +88,50 @@ def pathExists(): os.chmod(uptimes_db, 0o600) -def daemon(): +def daemon(fork = True): global uptime, now, s # fork - pid = os.fork() - if pid > 0: - sys.exit(0) - - os.chdir('/') - os.setsid() - os.umask(0) + if fork: + pid = os.fork() + if pid > 0: + sys.exit(0) + + os.chdir('/') + os.setsid() + os.umask(0) + # redirect + sys.stdout.flush() + sys.stderr.flush() + + si = open('/dev/null', 'r') + so = open('/dev/null', 'a+') + se = open('/dev/null', 'a+') + + os.dup2(si.fileno(), sys.stdin.fileno()) + os.dup2(so.fileno(), sys.stdout.fileno()) + os.dup2(se.fileno(), sys.stderr.fileno()) + # fork again + pid = os.fork() + if pid > 0: + sys.exit(0) + + # redirect + sys.stdout.flush() + sys.stderr.flush() + + si = open('/dev/null', 'r') + so = open('/dev/null', 'a+') + se = open('/dev/null', 'a+') + + os.dup2(si.fileno(), sys.stdin.fileno()) + os.dup2(so.fileno(), sys.stdout.fileno()) + os.dup2(se.fileno(), sys.stderr.fileno()) - # fork again - pid = os.fork() - if pid > 0: - sys.exit(0) # change process name import setproctitle setproctitle.setproctitle('uptimesd') - # redirect - sys.stdout.flush() - sys.stderr.flush() - - si = open('/dev/null', 'r') - so = open('/dev/null', 'a+') - se = open('/dev/null', 'a+') - - os.dup2(si.fileno(), sys.stdin.fileno()) - os.dup2(so.fileno(), sys.stdout.fileno()) - os.dup2(se.fileno(), sys.stderr.fileno()) - # create socket file try: os.unlink(socket_file) diff --git a/uptimesd.service b/uptimesd.service index 0810eff..de27dd3 100644 --- a/uptimesd.service +++ b/uptimesd.service @@ -3,8 +3,8 @@ Description=uptimes: status of total uptime After=network.target network-online.target [Service] -Type=forking +Type=simple User=root -ExecStart=/usr/bin/uptimes --daemon +ExecStart=/usr/bin/uptimes --systemd [Install] WantedBy=multi-user.target \ No newline at end of file