Untitled

 avatar
cion
plain_text
16 days ago
5.5 kB
1
Indexable
Never
#!/usr/bin/env bash

# List of services
SERVICES=(
    "data-uploader-consumer.service"
    "data-uploader-exposer.service"
    "data-uploader-file-fetch.service"
    "data-uploader-staging-writer.service"
    "data-uploader-writeback.service"
    "pulse-portal-backend.service"
    "pulse-portal-ui.service"
)

# User variable
USER="wmanltc"

# Functions
# Check if the script is run by the correct user
check_user() {
    if [ "$(whoami)" != "$USER" ]; then
        echo "This script must be run as $USER"
        exit 1
    fi
}

# Display the status of all services
display_status() {
    echo "Service Status:"
    for service in "${SERVICES[@]}"; do
        status=$(systemctl --user is-active "$service")
        echo "$service: $status"
    done
}

# Start a service
start_service() {
    systemctl --user start "$1"
    echo "$1 started."
}

# Stop a service
stop_service() {
    systemctl --user stop "$1"
    echo "$1 stopped."
}

# Enable a service
enable_service() {
    systemctl --user enable "$1"
    echo "$1 enabled."
}

# Disable a service
disable_service() {
    systemctl --user disable "$1"
    echo "$1 disabled."
}

# Start all services
start_all_services() {
    for service in "${SERVICES[@]}"; do
        start_service "$service"
    done
}

# Stop all services
stop_all_services() {
    for service in "${SERVICES[@]}"; do
        stop_service "$service"
    done
}

# Enable all services
enable_all_services() {
    for service in "${SERVICES[@]}"; do
        enable_service "$service"
    done
}

# Disable all services
disable_all_services() {
    for service in "${SERVICES[@]}"; do
        disable_service "$service"
    done
}

# Display menu
display_menu() {
    echo "Select an option:"
    echo "1. Display status of all services"
    echo "2. Start a service"
    echo "3. Stop a service"
    echo "4. Enable a service"
    echo "5. Disable a service"
    echo "6. Start all services"
    echo "7. Stop all services"
    echo "8. Enable all services"
    echo "9. Disable all services"
    echo "0. Exit"
}

# Display service selection menu
select_services() {
    echo "Select services (separate by space, e.g., 1 3 5):"
    for i in "${!SERVICES[@]}"; do
        echo "$((i+1)). ${SERVICES[$i]}"
    done
    read -r -a selected
    selected_services=()
    for i in "${selected[@]}"; do
        selected_services+=("${SERVICES[$((i-1))]}")
    done
    echo "${selected_services[@]}"
}

# main logic
main() {
    check_user

    while true; do
        display_menu
        read -r -p "Enter your choice: " choice

        case $choice in
            1) display_status ;;
            2) 
                services=($(select_services))
                for service in "${services[@]}"; do
                    start_service "$service"
                done
                ;;
            3) 
                services=($(select_services))
                for service in "${services[@]}"; do
                    stop_service "$service"
                done
                ;;
            4) 
                services=($(select_services))
                for service in "${services[@]}"; do
                    enable_service "$service"
                done
                ;;
            5) 
                services=($(select_services))
                for service in "${services[@]}"; do
                    disable_service "$service"
                done
                ;;
            6) start_all_services ;;
            7) stop_all_services ;;
            8) enable_all_services ;;
            9) disable_all_services ;;
            0) exit 0 ;;
            *) echo "Invalid choice. Please try again." ;;
        esac
    done
}

main

----------------------

bash-4.4$ ./pulse_service_manager.sh
Select an option:
1. Display status of all services
2. Start a service
3. Stop a service
4. Enable a service
5. Disable a service
6. Start all services
7. Stop all services
8. Enable all services
9. Disable all services
0. Exit
Enter your choice: 2

Failed to start Select.service: Unit Select.service not found.
Select started.
Failed to start services.service: Unit services.service not found.
services started.
Invalid unit name "(separate" was escaped as "\x28separate" (maybe you should use systemd-escape?)
Failed to start \x28separate.service: Unit \x28separate.service not found.
(separate started.
Failed to start by.service: Unit by.service not found.
by started.
Invalid unit name "space," was escaped as "space\x2c" (maybe you should use systemd-escape?)
Failed to start space\x2c.service: Unit space\x2c.service not found.
space, started.
Invalid unit name "e.g.," was escaped as "e.g.\x2c" (maybe you should use systemd-escape?)
Failed to start e.g.\x2c.service: Unit e.g.\x2c.service not found.
e.g., started.
Failed to start 1.service: Unit 1.service not found.
1 started.
Failed to start 3.service: Unit 3.service not found.
3 started.
Invalid unit name "5):" was escaped as "5\x29:" (maybe you should use systemd-escape?)
Failed to start 5\x29:.service: Unit 5\x29:.service not found.
5): started.
Failed to start 1..service: Unit 1..service not found.
1. started.
data-uploader-consumer.service started.
Failed to start 2..service: Unit 2..service not found.
2. started.
data-uploader-exposer.service started.
Failed to start 3..service: Unit 3..service not found.
Leave a Comment