From 4367495931cce78b86684a0815e14c0e5497ec41 Mon Sep 17 00:00:00 2001 From: Ali Mehraji Date: Fri, 13 Dec 2024 15:36:16 +0330 Subject: [PATCH] Update: changes for rm and pkill the app pid (#285) * Update: changes for rm and pkill the app pid --- README.md | 6 ++-- scripts/install.sh | 68 ++++++++++++++++++++++++++++++++++++-------- scripts/uninstall.sh | 65 +++++++++++++++++++++++++++++++++--------- 3 files changed, 109 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 05db84f..5f2b4b2 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ in order to download and install the app ### installation script (Linux) ```bash -curl -fsSL https://raw.githubusercontent.com/amir1376/ab-download-manager/master/scripts/install.sh | bash +bash <(curl -fsSL https://raw.githubusercontent.com/amir1376/ab-download-manager/master/scripts/install.sh) ``` ### winget or scoop (for Windows) @@ -60,14 +60,12 @@ winget install amir1376.ABDownloadManager scoop install extras/abdownloadmanager ``` - - ## Uninstall perform below command to uninstall ```bash -curl -fsSL https://raw.githubusercontent.com/amir1376/ab-download-manager/master/scripts/uninstall.sh | bash +bash <(curl -fsSL https://raw.githubusercontent.com/amir1376/ab-download-manager/master/scripts/uninstall.sh) ``` ## Screenshots diff --git a/scripts/install.sh b/scripts/install.sh index b5d2503..c5a9746 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -14,13 +14,29 @@ logger() { if [[ "$1" == "error" ]]; then # Red color for errors - echo -e "${timestamp} -- "$0" [Error]: \033[0;31m$@\033[0m" | tee -a ${LOG_FILE} + echo -e "${timestamp} -- ABDM-Installer [Error]: \033[0;31m$@\033[0m" | tee -a ${LOG_FILE} else # Default color for non-error messages - echo -e "${timestamp} -- "$0" [Info]: $@" | tee -a ${LOG_FILE} + echo -e "${timestamp} -- ABDM-Installer [Info]: $@" | tee -a ${LOG_FILE} fi } +remove_if_exists() { + local target="$1" + + if [ -z "$target" ]; then + logger "No target specified in remove_if_exists function" + return 1 + fi + + if [ -e "$target" ]; then + logger "File \"$target\" Removed" + rm -rf "$target" + else + logger "File \"$target\" does not exist" + fi +} + # --- Detect OS and The Package Manager to use detect_package_manager() { if [ -f /etc/os-release ]; then @@ -101,11 +117,39 @@ BINARY_PATH="$HOME/.local/$APP_NAME/bin/$APP_NAME" # --- Delete the old version Application if exists delete_old_version() { - # --- Killing Any Application Process - pkill -f "$APP_NAME" - rm -rf "$HOME/.local/$APP_NAME" - rm -rf "$HOME/.local/bin/$APP_NAME" - logger "removed old version AB Download Manager" + # Find the PID(s) of the application + PIDS=$(pidof "$APP_NAME") || true + + if [ -n "$PIDS" ]; then + echo "Found $APP_NAME with PID(s): $PIDS. Attempting to kill..." + + # Attempt to terminate the process gracefully + kill $PIDS 2>/dev/null || echo "Graceful kill failed..." + + # Wait for a short period to allow graceful shutdown + sleep 2 + + # Check if the process is still running + PIDS=$(pidof "$APP_NAME") || true + if [ -n "$PIDS" ]; then + echo "Process still running. Force killing..." + kill -9 $PIDS 2>/dev/null || echo "Force kill failed..." + else + echo "$APP_NAME terminated successfully." + fi + else + echo "$APP_NAME is not running." + fi + + # Remove old version directories + # First Remove link to "$HOME/.local/$APP_NAME" + remove_if_exists "$HOME/.local/bin/$APP_NAME" + # then Remove the main binary files directory + remove_if_exists "$HOME/.local/$APP_NAME" + + + # Log the removal action + logger "Removed old version of $APP_NAME" } # --- Generate a .desktop file for the app @@ -127,7 +171,7 @@ EOF # --- Download the latest version of the app download_zip() { # Remove the app tarball if it exists in /tmp - rm -f "/tmp/$ASSET_NAME" + remove_if_exists "/tmp/$ASSET_NAME" logger "downloading AB Download Manager ..." # Perform the download with curl @@ -135,9 +179,9 @@ download_zip() { logger "download finished successfully" else logger error "Download failed! Something Went Wrong" - logger error "Hint: Check Your Internet Connectivity" + logger error "Check Your Internet Connectivity" # Optionally remove the partially downloaded file - rm -f "/tmp/$ASSET_NAME" + remove_if_exists "/tmp/$ASSET_NAME" fi } @@ -151,10 +195,10 @@ install_app() { tar -xzf "/tmp/$ASSET_NAME" -C "$HOME/.local" # --- remove tarball after installation - rm "/tmp/$ASSET_NAME" + remove_if_exists "/tmp/$ASSET_NAME" # Link the binary to ~/.local/bin - ln -s "$BINARY_PATH" "$HOME/.local/bin/$APP_NAME" + ln -sf "$BINARY_PATH" "$HOME/.local/bin/$APP_NAME" # Create a .desktop file in ~/.local/share/applications generate_desktop_file diff --git a/scripts/uninstall.sh b/scripts/uninstall.sh index 7e6dcc2..87c4bef 100755 --- a/scripts/uninstall.sh +++ b/scripts/uninstall.sh @@ -14,13 +14,29 @@ logger() { if [[ "$1" == "error" ]]; then # Red color for errors - echo -e "${timestamp} -- "$0" [Error]: \033[0;31m$@\033[0m" | tee -a ${LOG_FILE} + echo -e "${timestamp} -- ABDM-Uninstaller [Error]: \033[0;31m$@\033[0m" | tee -a ${LOG_FILE} else # Default color for non-error messages - echo -e "${timestamp} -- "$0" [Info]: $@" | tee -a ${LOG_FILE} + echo -e "${timestamp} -- ABDM-Uninstaller [Info]: $@" | tee -a ${LOG_FILE} fi } +remove_if_exists() { + local target="$1" + + if [ -z "$target" ]; then + logger "No target specified in remove_if_exists function" + return 1 + fi + + if [ -e "$target" ]; then + logger "File \"$target\" Removed" + rm -rf "$target" + else + logger "File \"$target\" does not exist" + fi +} + delete_app_config_dir() { local answer @@ -29,8 +45,7 @@ delete_app_config_dir() { case $answer in [Yy]* ) - rm -rf "$HOME/.abdm" - logger "$APP_NAME settings and download lists directory: $HOME/.abdm removed." + remove_if_exists "$HOME/.abdm" ;; [Nn]* ) logger "Remove The $HOME/.abdm directory manually." @@ -44,25 +59,47 @@ delete_app_config_dir() { delete_app() { - logger "Killing Any $APP_NAME Processes ..." - pkill -f "$APP_NAME" + # Find the PID(s) of the application + PIDS=$(pidof "$APP_NAME") || true + + if [ -n "$PIDS" ]; then + echo "Found $APP_NAME with PID(s): $PIDS. Attempting to kill..." + + # Attempt to terminate the process gracefully + kill $PIDS 2>/dev/null || echo "Graceful kill failed..." + + # Wait for a short period to allow graceful shutdown + sleep 2 + + # Check if the process is still running + PIDS=$(pidof "$APP_NAME") || true + if [ -n "$PIDS" ]; then + echo "Process still running. Force killing..." + kill -9 $PIDS 2>/dev/null || echo "Force kill failed..." + else + echo "$APP_NAME terminated successfully." + fi + else + echo "$APP_NAME is not running." + fi logger "removing $APP_NAME desktop file ..." # --- Remove the .desktop file in ~/.local/share/applications - rm "$HOME/.local/share/applications/abdownloadmanager.desktop" + remove_if_exists "$HOME/.local/share/applications/abdownloadmanager.desktop" - logger "unlinking $APP_NAME link ..." - unlink "$HOME/.local/bin/$APP_NAME" + logger "removing $APP_NAME link ..." + remove_if_exists "$HOME/.local/bin/$APP_NAME" logger "removing $APP_NAME binary ..." - rm -rf "$HOME/.local/$APP_NAME" - rm -rf "$HOME/.local/bin/$APP_NAME" + remove_if_exists "$HOME/.local/$APP_NAME" logger "removing $APP_NAME autostart at boot file ..." - rm -f "$HOME/.config/autostart/AB Download Manager.desktop" + remove_if_exists "$HOME/.config/autostart/AB Download Manager.desktop" - logger "removing $APP_NAME settings and download lists" - delete_app_config_dir + if [ -e "$HOME/.abdm" ]; then + logger "removing $APP_NAME settings and download lists $HOME/.abdm" + delete_app_config_dir + fi logger "AB Download Manager completely removed" }