Fix/installation (#215)

* fix installation when using service - no need for ctrl+c

* Improve uninstallatino process
This commit is contained in:
Masoko
2025-04-12 13:48:49 +03:00
committed by GitHub
parent 9795ed7e10
commit 4387d40f5f
4 changed files with 53 additions and 8 deletions

View File

@@ -18,6 +18,7 @@ The easiest way to track your Raspberry Pi or Ubuntu computer system health and
* Remotely restart / shutdown your system and control your monitors. * Remotely restart / shutdown your system and control your monitors.
* Automatic HASS configuration: Supports discovery messages, so no manual configuration in [Home Assistant](https://www.home-assistant.io/) configuration.yaml is needed. * Automatic HASS configuration: Supports discovery messages, so no manual configuration in [Home Assistant](https://www.home-assistant.io/) configuration.yaml is needed.
* Automated installation and configuration: you can install it and schedule it with a service or cron with just one command from shell. * Automated installation and configuration: you can install it and schedule it with a service or cron with just one command from shell.
* Easy uninstallation, just run rpi-mqtt-monitor --uninstall
* Configurable: You can select what is monitored and how the message(s) is send (separately or as one csv message). * Configurable: You can select what is monitored and how the message(s) is send (separately or as one csv message).
* Easy update: You can update the script by calling it with command line "rpi-mqtt-monitor --update" or via Home Assistant UI. * Easy update: You can update the script by calling it with command line "rpi-mqtt-monitor --update" or via Home Assistant UI.
* Support multiple languages: English, German and Bulgarian * Support multiple languages: English, German and Bulgarian
@@ -63,7 +64,7 @@ The easiest way to track your Raspberry Pi or Ubuntu computer system health and
## CLI arguments ## CLI arguments
``` ```
usage: rpi-mqtt-monitor [-h] [-H] [-d] [-s] [-v] [-u] [-w] usage: rpi-mqtt-monitor [-h] [-H] [-d] [-s] [-v] [-u] [-w] [--uninstall]
Monitor CPU load, temperature, frequency, free space, etc., and publish the data to an MQTT server or Home Assistant API. Monitor CPU load, temperature, frequency, free space, etc., and publish the data to an MQTT server or Home Assistant API.
@@ -75,6 +76,7 @@ options:
-v, --version display installed version and exit -v, --version display installed version and exit
-u, --update update script and config then exit -u, --update update script and config then exit
-w, --hass_wake display Home assistant wake on lan configuration -w, --hass_wake display Home assistant wake on lan configuration
--uninstall uninstall rpi-mqtt-monitor and remove all related files
``` ```
@@ -106,6 +108,13 @@ It is recommended to run the script as a service, this way you can use the resta
### Manual ### Manual
[moved to wiki](../../wiki/Manual-Installation) [moved to wiki](../../wiki/Manual-Installation)
## Uninstallation
To uninstall Raspberry Pi MQTT Monitor, run the following command:
'''rpi-mqtt-monitor --uninstall'''
## Home Assistant Integration ## Home Assistant Integration
If you are using discovery_messages, then this step is not required as a new MQTT device will be automatically created in Home Assistant and all you need to do is add it to a dashboard. If you are using discovery_messages, then this step is not required as a new MQTT device will be automatically created in Home Assistant and all you need to do is add it to a dashboard.

View File

@@ -237,7 +237,6 @@ set_service(){
sudo systemctl enable rpi-mqtt-monitor.service sudo systemctl enable rpi-mqtt-monitor.service
sudo systemctl start rpi-mqtt-monitor.service sudo systemctl start rpi-mqtt-monitor.service
sudo service rpi-mqtt-monitor restart sudo service rpi-mqtt-monitor restart
sudo service rpi-mqtt-monitor status
print_green "+ Service is enabled and started" print_green "+ Service is enabled and started"
git config --global --add safe.directory ${cwd} git config --global --add safe.directory ${cwd}
} }

View File

@@ -24,15 +24,29 @@ welcome(){
uninstall(){ uninstall(){
printm "Uninstalling rpi-mqtt-monitor" printm "Uninstalling rpi-mqtt-monitor"
# Remove the rpi-mqtt-monitor directory # Ask for confirmation before proceeding
if [ -d "rpi-mqtt-monitor" ]; then read -r -p "Are you sure you want to uninstall rpi-mqtt-monitor? [y/N] " response
rm -rf rpi-mqtt-monitor if [[ ! "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo "Uninstallation canceled."
exit
fi
# Get the absolute path of the script
script_dir=$(dirname "$(realpath "$0")")
# Remove the rpi-mqtt-monitor directory if it exists
if [ -d "$script_dir" ]; then
if [ "$(realpath rpi-mqtt-monitor)" == "$script_dir" ]; then
# If the script is running from the installation directory, navigate out of it
cd ..
fi
sudo rm -rf "$script_dir"
echo "Removed rpi-mqtt-monitor directory." echo "Removed rpi-mqtt-monitor directory."
else else
echo "rpi-mqtt-monitor directory not found." echo "rpi-mqtt-monitor directory not found."
fi fi
# Remove the cron job if it exists # Remove the cron job if it exists
if crontab -l | grep -q rpi-cpu2mqtt.py; then if crontab -l | grep -q rpi-cpu2mqtt.py; then
crontab -l | grep -v rpi-cpu2mqtt.py | crontab - crontab -l | grep -v rpi-cpu2mqtt.py | crontab -

View File

@@ -869,7 +869,7 @@ def parse_arguments():
parser.add_argument('-v', '--version', action='store_true', help='display installed version and exit', default=False) parser.add_argument('-v', '--version', action='store_true', help='display installed version and exit', default=False)
parser.add_argument('-u', '--update', action='store_true', help='update script and config then exit', default=False) parser.add_argument('-u', '--update', action='store_true', help='update script and config then exit', default=False)
parser.add_argument('-w', '--hass_wake', action='store_true', help='display Home assistant wake on lan configuration', default=False) parser.add_argument('-w', '--hass_wake', action='store_true', help='display Home assistant wake on lan configuration', default=False)
parser.add_argument('--uninstall', action='store_true', help='uninstall rpi-mqtt-monitor and remove all related files')
args = parser.parse_args() args = parser.parse_args()
if args.update: if args.update:
@@ -1015,6 +1015,24 @@ def update_status():
break break
def uninstall_script():
"""Call the remote_install.sh script to uninstall the application."""
script_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../remote_install.sh")
if not os.path.exists(script_path):
print("Error: remote_install.sh script not found.")
return
try:
# Run the uninstall command
subprocess.run(["bash", script_path, "uninstall"], check=True)
print("Uninstallation process completed.")
except subprocess.CalledProcessError as e:
print(f"Error during uninstallation: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
def on_message(client, userdata, msg): def on_message(client, userdata, msg):
global exit_flag, thread1, thread2 global exit_flag, thread1, thread2
print("Received message: ", msg.payload.decode()) print("Received message: ", msg.payload.decode())
@@ -1060,6 +1078,11 @@ else:
if __name__ == '__main__': if __name__ == '__main__':
args = parse_arguments(); args = parse_arguments();
if args.uninstall:
uninstall_script()
sys.exit(0)
if args.service: if args.service:
if not args.hass_api: if not args.hass_api:
client = paho.Client() client = paho.Client()