Add support for execution as service (--service,-s)

This commit is contained in:
Hristo
2024-01-27 22:14:06 +02:00
committed by Masoko
parent dba655668c
commit 8a65c3c71c
4 changed files with 81 additions and 58 deletions

View File

@@ -96,13 +96,14 @@ Populate the variables for MQTT host, user, password and main topic in ```src/co
You can also choose what messages are sent and what is the delay (sleep_time is only used for multiple messages) between them. You can also choose what messages are sent and what is the delay (sleep_time is only used for multiple messages) between them.
If you are sending a grouped message, and you want to delay the execution of the script you need to use the ```random_delay``` variable which is set to 1 by default. If you are sending a grouped message, and you want to delay the execution of the script you need to use the ```random_delay``` variable which is set to 1 by default.
This is the default configuration: This is the default configuration (check the example file for more info):
``` ```
random_delay = randrange(1) random_delay = randrange(1)
discovery_messages = True discovery_messages = True
group_messages = False group_messages = False
sleep_time = 0.5 sleep_time = 0.5
service_sleep_time = 120
cpu_load = True cpu_load = True
cpu_temp = True cpu_temp = True
used_space = True used_space = True
@@ -113,7 +114,6 @@ memory = True
uptime = True uptime = True
wifi_signal = False wifi_signal = False
wifi_signal_dbm = False wifi_signal_dbm = False
# this works only on raspbery pi version 5 with stock fan
rpi5_fan_speed = False rpi5_fan_speed = False
``` ```

14
rpi-mqtt-monitor.service Normal file
View File

@@ -0,0 +1,14 @@
[Unit]
Description=RPI MQTT Monitor
After=network.target
[Service]
ExecStart=/home/masoko/git/rpi-mqtt-monitor/rpi_mon_env/bin/python /home/masoko/git/rpi-mqtt-monitor/src/rpi-cpu2mqtt.py --service
WorkingDirectory=/home/masoko/git/rpi-mqtt-monitor/
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target

View File

@@ -30,6 +30,9 @@ qos = 0
# - if you want a fixed delay you can remove the randrange function and just set the needed value. # - if you want a fixed delay you can remove the randrange function and just set the needed value.
# random_delay = randrange(10) # random_delay = randrange(10)
# this is the time between executiuons if the script is used as service (--service option)
service_sleep_time = 120
# This is the time between sending the individual messages # This is the time between sending the individual messages
sleep_time = 0.1 sleep_time = 0.1

View File

@@ -360,11 +360,11 @@ def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_cl
if __name__ == '__main__': if __name__ == '__main__':
# parse arguments # parse arguments
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-d', action='store_true', help='Display values on screen', default=False) parser.add_argument('--display', '-d', action='store_true', help='Display values on screen', default=False)
parser.add_argument('--service', '-s', action='store_true', help='Run script as a service', default=False)
args = parser.parse_args() args = parser.parse_args()
while True:
# set all monitored values to False in case they are turned off in the config # set all monitored values to False in case they are turned off in the config
cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_days = wifi_signal = wifi_signal_dbm = rpi5_fan_speed = git_update = False cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_days = wifi_signal = wifi_signal_dbm = rpi5_fan_speed = git_update = False
@@ -400,7 +400,7 @@ if __name__ == '__main__':
if config.git_update: if config.git_update:
git_update = check_git_update() git_update = check_git_update()
if args.d: if args.display:
print("Hostname: " + hostname) print("Hostname: " + hostname)
print("CPU Load: " + str(cpu_load)) print("CPU Load: " + str(cpu_load))
print("CPU Temp: " + str(cpu_temp)) print("CPU Temp: " + str(cpu_temp))
@@ -414,10 +414,16 @@ if __name__ == '__main__':
print("Wifi Signal dBm: " + str(wifi_signal_dbm)) print("Wifi Signal dBm: " + str(wifi_signal_dbm))
print("RPI5 Fan Speed: " + str(rpi5_fan_speed)) print("RPI5 Fan Speed: " + str(rpi5_fan_speed))
print("Git Update: " + str(git_update)) print("Git Update: " + str(git_update))
print("")
# Publish messages to MQTT # Publish messages to MQTT
if hasattr(config, 'group_messages') and config.group_messages: if hasattr(config, 'group_messages') and config.group_messages:
bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update) bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update)
else: else:
publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update) publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update)
# if not running as a service, break the loop after one iteration
if not args.service:
break
# if running as a service, sleep for 2 minutes before the next iteration
time.sleep(config.service_sleep_time)