Browse Source

Added systemd unit file

master
Torsten Kurbad 3 years ago
parent
commit
48de4faf90
3 changed files with 26 additions and 8 deletions
  1. +6
    -0
      README.md
  2. +10
    -8
      pyupsmon.py
  3. +10
    -0
      pyupsmon.service

+ 6
- 0
README.md View File

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

+ 10
- 8
pyupsmon.py View File

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

+ 10
- 0
pyupsmon.service View File

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

Loading…
Cancel
Save