HEX
Server: Apache
System: Linux hz.vslconceptsdomains.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: dkfounda (3233)
PHP: 8.1.34
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/share/imunify360/scripts/set-service-resources.sh
#!/bin/bash
#
# Set or unset systemd service resource properties
#
# Usage: set-service-resources.sh SERVICE_NAME CPU_QUOTA MEMORY_HIGH
#
# Arguments:
#   SERVICE_NAME - Name of the systemd service (e.g., "imunify360.service")
#   CPU_QUOTA    - CPU quota value (e.g., "50" for 50%) or empty to unset
#   MEMORY_HIGH  - Memory high value (e.g., "50" for 50%) or empty to unset
#
# Examples:
#   set-service-resources.sh imunify360.service 50 50        # Set limits
#   set-service-resources.sh imunify360.service "" ""        # Unset limits
#

set -e

if [[ $# -ne 3 ]]; then
    echo "Usage: $0 SERVICE_NAME CPU_QUOTA MEMORY_HIGH" >&2
    echo "Example: $0 imunify360.service 50% 50%" >&2
    echo "Example: $0 imunify360.service \"\" \"\"  # to unset" >&2
    exit 1
fi

SERVICE_NAME="$1"
CPU_QUOTA="$2"
MEMORY_HIGH="$3"

if [[ -z "$SERVICE_NAME" ]]; then
    echo "Error: SERVICE_NAME cannot be empty" >&2
    exit 1
fi

get_systemd_version() {
    /bin/systemctl --version | head -n1 | grep -oE '[0-9]+' | head -n1
}

SYSTEMD_VERSION=$(get_systemd_version)
MEMORY_HIGH_MIN_VERSION=232


if [[ -n "$CPU_QUOTA" ]]; then
    echo "$SERVICE_NAME: Setting CPUQuota=$CPU_QUOTA%"
    /bin/systemctl set-property --runtime "$SERVICE_NAME" CPUQuota="$CPU_QUOTA%"
else
    echo "$SERVICE_NAME: Unsetting CPUQuota"
    /bin/systemctl set-property --runtime "$SERVICE_NAME" CPUQuota=
fi

if [[ "$SYSTEMD_VERSION" -ge "$MEMORY_HIGH_MIN_VERSION" ]]; then
    if [[ -n "$MEMORY_HIGH" ]]; then
        echo "$SERVICE_NAME: Setting MemoryHigh=$MEMORY_HIGH%"
        /bin/systemctl set-property --runtime "$SERVICE_NAME" MemoryHigh="$MEMORY_HIGH%"
    else
        echo "$SERVICE_NAME: Unsetting MemoryHigh"
        /bin/systemctl set-property --runtime "$SERVICE_NAME" MemoryHigh=
    fi
else
    if [[ -n "$MEMORY_HIGH" ]]; then
        echo "$SERVICE_NAME: MemoryHigh not supported on systemd v${SYSTEMD_VERSION} (requires v${MEMORY_HIGH_MIN_VERSION}+), skipping"
    fi
fi

echo "$SERVICE_NAME: Resource configuration completed"

exit 0