diff --git a/README.md b/README.md index 1bac2e4..0c0b01c 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,12 @@ sudo emerge -NDuv pyyaml sudo cp pyupsmon.yml.example /etc/pyupsmon.yml ``` * Edit the config to your liking using your favourite text editor +* Install the Python script +``` +sudo mkdir -p /usr/local/sbin +sudo cp pyupsmon.py /usr/local/sbin/pyupsmon +sudo chmod 0755 /usr/local/sbin/pyupsmon +``` * Install the `systemd` unit file ``` sudo cp pyupsmon.service /etc/systemd/system diff --git a/pyupsmon.py b/pyupsmon.py index 3a8403e..2f29be5 100644 --- a/pyupsmon.py +++ b/pyupsmon.py @@ -52,7 +52,7 @@ class ConvertUPS(Serial): if self.shutdownTimeout is None: self.shutdownTimeout = float(cfg['shutdown'].get('timeout', 3600)) if self.shutdownCommand is None: - self.shutdownCommand = shlex.split(cfg['shutdown'].get('command', '/sbin/shutdown -h now')) + self.shutdownCommand = shlex.split(cfg['shutdown'].get('command', '/sbin/shutdown -h +2')) except OSError: if debug: print('Could not read config file %s.' % configfile) @@ -68,7 +68,7 @@ class ConvertUPS(Serial): if self.shutdownTimeout is None: self.shutdownTimeout = 3600.0 if self.shutdownCommand is None: - self.shutdownCommand = shlex.split('/sbin/shutdown -h now') + self.shutdownCommand = shlex.split('/sbin/shutdown -h +2') if self.debug: print('Configuration:') @@ -189,12 +189,12 @@ class ConvertUPS(Serial): if __name__ == "__main__": - #try: - ups = ConvertUPS() - #except: - # print('Error: Could not open UPS serial connection.') - # print('\tCheck the config file!') - # sys.exit(1) + try: + ups = ConvertUPS() + except: + print('Error: Could not open UPS serial connection.') + print('\tCheck the config file!') + sys.exit(1) if not ups.upsInit(): print('Error: Could not initialize UPS.') @@ -204,7 +204,9 @@ if __name__ == "__main__": (toggled, onBatt) = ups.powerToggled() if toggled: if onBatt: + print('UPS AC power loss. Starting shutdown timer.') ups.startShutdownTimer() if not onBatt: + print('UPS AC power restored. Cancelling shutdown timer.') ups.cancelShutdownTimer() sleep(ups.interval) diff --git a/pyupsmon.service b/pyupsmon.service new file mode 100644 index 0000000..23b220b --- /dev/null +++ b/pyupsmon.service @@ -0,0 +1,10 @@ +[Unit] +Description=Python3 UPS Monitor +Documentation=https://git.iwm-tuebingen.de/tkurbad/pyupsmon + +[Service] +Type=simple +ExecStart=/usr/local/sbin/pyupsmon + +[Install] +WantedBy=multi-user.target