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>
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"

63
uptimes
View File

@ -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)

View File

@ -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