fix systemd runtime

This commit is contained in:
iridiumR 2023-08-07 21:46:14 +08:00
parent 98d4802871
commit 64ac9ad155
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8
3 changed files with 44 additions and 29 deletions

View file

@ -1,6 +1,6 @@
# Maintainer: 1ridic <i@8f.al> # Maintainer: 1ridic <i@8f.al>
pkgname=uptimes pkgname=uptimes
pkgver=0.1.2 pkgver=0.1.3
pkgrel=2 pkgrel=2
pkgdesc="status of total uptime" pkgdesc="status of total uptime"
arch=('any') arch=('any')
@ -11,8 +11,8 @@ source=(
local://uptimes local://uptimes
local://uptimesd.service local://uptimesd.service
) )
md5sums=('04c97379f5910c5c7fdcea9b6b2e0024' md5sums=('e70411e89528c1d4f55fdbf97c4e4736'
'f006fee00a5269c916d7bacddfe7035c') '7a90889f87792317e70ebdce9dae7023')
package() { package() {
install -Dm755 uptimes "$pkgdir/usr/bin/uptimes" install -Dm755 uptimes "$pkgdir/usr/bin/uptimes"

25
uptimes
View file

@ -18,11 +18,14 @@ def main():
global uptime, s global uptime, s
parse = argparse.ArgumentParser(description='Uptimes') parse = argparse.ArgumentParser(description='Uptimes')
parse.add_argument('-d', '--daemon', action='store_true', help='run as daemon') 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() args = parse.parse_args()
if args.daemon: if args.daemon:
daemon() daemon()
elif args.systemd:
daemon(fork=False)
else: else:
# check socket file # check socket file
if not os.path.exists(socket_file): if not os.path.exists(socket_file):
@ -85,9 +88,10 @@ def pathExists():
os.chmod(uptimes_db, 0o600) os.chmod(uptimes_db, 0o600)
def daemon(): def daemon(fork = True):
global uptime, now, s global uptime, now, s
# fork # fork
if fork:
pid = os.fork() pid = os.fork()
if pid > 0: if pid > 0:
sys.exit(0) sys.exit(0)
@ -95,16 +99,22 @@ def daemon():
os.chdir('/') os.chdir('/')
os.setsid() os.setsid()
os.umask(0) 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 # fork again
pid = os.fork() pid = os.fork()
if pid > 0: if pid > 0:
sys.exit(0) sys.exit(0)
# change process name
import setproctitle
setproctitle.setproctitle('uptimesd')
# redirect # redirect
sys.stdout.flush() sys.stdout.flush()
sys.stderr.flush() sys.stderr.flush()
@ -117,6 +127,11 @@ def daemon():
os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno()) os.dup2(se.fileno(), sys.stderr.fileno())
# change process name
import setproctitle
setproctitle.setproctitle('uptimesd')
# create socket file # create socket file
try: try:
os.unlink(socket_file) os.unlink(socket_file)

View file

@ -3,8 +3,8 @@ Description=uptimes: status of total uptime
After=network.target network-online.target After=network.target network-online.target
[Service] [Service]
Type=forking Type=simple
User=root User=root
ExecStart=/usr/bin/uptimes --daemon ExecStart=/usr/bin/uptimes --systemd
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target