diff --git a/dd-java-agent/agent-crashtracking/src/main/resources/com/datadog/crashtracking/notify_oome.sh b/dd-java-agent/agent-crashtracking/src/main/resources/com/datadog/crashtracking/notify_oome.sh index c2ee9ecf6df..5e75f6a7563 100644 --- a/dd-java-agent/agent-crashtracking/src/main/resources/com/datadog/crashtracking/notify_oome.sh +++ b/dd-java-agent/agent-crashtracking/src/main/resources/com/datadog/crashtracking/notify_oome.sh @@ -1,51 +1,64 @@ -#!/usr/bin/env bash +#!/bin/sh -set +e # Disable exit on error +# Disable exit on error +set +e # Check if PID is provided if [ -z "$1" ]; then echo "Error: No PID provided" exit 1 fi -HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" # Get the directory of the script + +# Get the directory of the script +HERE=$(cd "$(dirname "$0")" >/dev/null 2>&1 && pwd) PID=$1 -# Get the base name of the script -scriptName=$(basename "$0" .sh) +# Get the base name of the script (without .sh) +scriptName=$(basename "$0") +scriptName=${scriptName%.sh} + configFile="${HERE}/${scriptName}_pid${PID}.cfg" if [ ! -f "$configFile" ]; then echo "Error: Configuration file not found: $configFile" exit 1 fi +# Initialize config values +config_agent="" +config_tags="" +config_java_home="" + # Read the configuration file -# The expected contents are: -# - agent: Path to the agent jar -# - tags: Comma-separated list of tags to be sent with the OOME event; key:value pairs are supported while IFS="=" read -r key value; do - declare "config_$key"="$value" + case "$key" in + agent) config_agent=$value ;; + tags) config_tags=$value ;; + java_home) config_java_home=$value ;; + esac done < "$configFile" # Exiting early if configuration is missing -if [ -z "${config_agent}" ] || [ -z "${config_tags}" ] || [ -z "${config_java_home}" ]; then +if [ -z "$config_agent" ] || [ -z "$config_tags" ] || [ -z "$config_java_home" ]; then echo "Error: Missing configuration" exit 1 fi # Debug: Print the loaded values (Optional) -echo "Agent Jar: ${config_agent}" -echo "Tags: ${config_tags}" -echo "JAVA_HOME: ${config_java_home}" +echo "Agent Jar: $config_agent" +echo "Tags: $config_tags" +echo "JAVA_HOME: $config_java_home" echo "PID: $PID" # Execute the Java command with the loaded values -"${config_java_home}/bin/java" -Ddd.dogstatsd.start-delay=0 -jar "${config_agent}" sendOomeEvent "${config_tags}" +"$config_java_home/bin/java" -Ddd.dogstatsd.start-delay=0 -jar "$config_agent" sendOomeEvent "$config_tags" RC=$? -rm -f "${configFile}" # Remove the configuration file -if [ $RC -eq 0 ]; then +# Remove the configuration file +rm -f "$configFile" + +if [ "$RC" -eq 0 ]; then echo "OOME Event generated successfully" else echo "Error: Failed to generate OOME event" - exit $RC + exit "$RC" fi diff --git a/dd-java-agent/agent-crashtracking/src/main/resources/com/datadog/crashtracking/upload_crash.sh b/dd-java-agent/agent-crashtracking/src/main/resources/com/datadog/crashtracking/upload_crash.sh index e2d0f378797..9d6c5b8180a 100644 --- a/dd-java-agent/agent-crashtracking/src/main/resources/com/datadog/crashtracking/upload_crash.sh +++ b/dd-java-agent/agent-crashtracking/src/main/resources/com/datadog/crashtracking/upload_crash.sh @@ -1,6 +1,7 @@ -#!/usr/bin/env bash +#!/bin/sh -set +e # Disable exit on error +# Disable exit on error +set +e # Check if PID is provided if [ -z "$1" ]; then @@ -16,45 +17,56 @@ if [ -z "$1" ]; then exit 0 fi -HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" # Get the directory of the script +# Get the directory of the script +HERE=$(cd "$(dirname "$0")" >/dev/null 2>&1 && pwd) PID=$1 -# Get the base name of the script -scriptName=$(basename "$0" .sh) +# Get the base name of the script (without .sh) +scriptName=$(basename "$0") +scriptName=${scriptName%.sh} + configFile="${HERE}/${scriptName}_pid${PID}.cfg" if [ ! -f "$configFile" ]; then - echo "Error: Configuration file not found: $configFile" - exit 1 + echo "Error: Configuration file not found: $configFile" + exit 1 fi +# Initialize config values +config_agent="" +config_hs_err="" +config_java_home="" + # Read the configuration file -# The expected contents are: -# - agent: Path to the agent jar -# - hs_err: Path to the hs_err log file while IFS="=" read -r key value; do - declare "config_$key"="$value" + case "$key" in + agent) config_agent=$value ;; + hs_err) config_hs_err=$value ;; + java_home) config_java_home=$value ;; + esac done < "$configFile" # Exiting early if configuration is missing -if [ -z "${config_agent}" ] || [ -z "${config_hs_err}" ] || [ -z "${config_java_home}" ]; then - echo "Error: Missing configuration" - exit 1 +if [ -z "$config_agent" ] || [ -z "$config_hs_err" ] || [ -z "$config_java_home" ]; then + echo "Error: Missing configuration" + exit 1 fi # Debug: Print the loaded values (Optional) -echo "Agent Jar: ${config_agent}" -echo "Error Log: ${config_hs_err}" -echo "JAVA_HOME: ${config_java_home}" +echo "Agent Jar: $config_agent" +echo "Error Log: $config_hs_err" +echo "JAVA_HOME: $config_java_home" echo "PID: $PID" # Execute the Java command with the loaded values -"${config_java_home}/bin/java" -jar "${config_agent}" uploadCrash "${config_hs_err}" +"$config_java_home/bin/java" -jar "$config_agent" uploadCrash "$config_hs_err" RC=$? -rm -f "${configFile}" # Remove the configuration file -if [ $RC -eq 0 ]; then - echo "Error file ${config_hs_err} was uploaded successfully" +# Remove the configuration file +rm -f "$configFile" + +if [ "$RC" -eq 0 ]; then + echo "Error file $config_hs_err was uploaded successfully" else - echo "Error: Failed to upload error file ${config_hs_err}" - exit $RC + echo "Error: Failed to upload error file $config_hs_err" + exit "$RC" fi