我的程序没有通过systemd

时间:2017-12-13 16:47:36

标签: python-3.x debian systemd battery gdbus

我在Python3中开发了一个小程序来测试我的电池电量,并在超过阈值时发送通知。

这是我的代码:

#!/usr/bin/env python3

import configparser, signal
from subprocess import run
from sys import exit
from time import sleep
from gi.repository import Gio

CHARGE_NOW = "/sys/class/power_supply/BAT0/charge_now"
CHARGE_FULL = "/sys/class/power_supply/BAT0/charge_full"

#####
## Load configuration file
configfile = "./check_battery.conf"
try:
    cfg = configparser.ConfigParser()
    cfg.read(configfile)
except FileNotFoundError:
    print("No found configuration file")
    exit(1)
except:
    print("Error to load configuration file")
    exit(1)

def notification(text):
    Application=Gio.Application.new ("check.battery", Gio.ApplicationFlags.FLAGS_NONE);
    Application.register ();
    Notification=Gio.Notification.new ("BATTERY");
    Notification.set_body (text);
    Icon=Gio.ThemedIcon.new ("dialog-information");
    Notification.set_icon (Icon);
    Application.send_notification (None, Notification);

def readSysFile(file_path):
    with open(file_path, "r") as fp:
        return fp.read()

def readPercBat():
    return int(100 * int(readSysFile(CHARGE_NOW)) / int(readSysFile(CHARGE_FULL)))

def mainloop():
    delay = int(cfg['BAT']['refresh'])
    perc_to_warning = int(cfg['BAT']['perc_to_warning'])
    perc_to_shutdown = int(cfg['BAT']['perc_to_shutdown'])
    while True:
        if readPercBat() <= perc_to_warning \
        and readPercBat() > perc_to_shutdown:
            notification("Battery is low")
            perc_to_warning = readPercBat() - int(cfg['BAT']['step_after_warning'])
        elif readPercBat() <= perc_to_shutdown:
            notification("Shutdown now")
            run(["shutdown", "now"])
        sleep(int(cfg['BAT']['refresh']))

def quit(signal, frame):
    exit(0)
signal.signal(signal.SIGINT, quit)
signal.signal(signal.SIGTERM, quit)


if __name__ == "__main__":
    mainloop()


# vim: ft=python ts=8 et sw=4 sts=4

当我在当地跑步时,没关系。我为systemd创建了一个脚本.service,以便在我登录会话时启动它。

这是我的脚本.service:

[Unit]
Description=Test l état de la batterie et ouvre une fenêtre quand elle est trop basse
After=graphical.target

[Service]
Type=simple
ExecStart=/usr/local/sbin/check_battery.py
ExecReload=/usr/bin/kill -HUP $MAINPID
PIDFile=/var/run/check_battery.pid
Restart=on-failure
User=romain

[Install]
WantedBy=graphical.target


# vim: ft=systemd

没关系,我可以毫无问题地开始。一旦他发送通知,它就不会显示,我有这些错误:

g_dbus_connection_signal_subscribe: assertion 'G_IS1_DBUS_CONNECTION (connection)' failed
g_dbus_connection_call_internal: assertion 'G_IS_DBUS_CONNECTION (connection)' failed

我不知道如何解决我的问题,这个想法?

0 个答案:

没有答案