Added systemd unit file

This commit is contained in:
Torsten Kurbad 2017-09-07 16:53:13 +02:00
parent 3f1d2e2d60
commit 48de4faf90
3 changed files with 26 additions and 8 deletions

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

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
pyupsmon.service Normal file
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