diff --git a/run_agent.sh b/run_agent.sh
index 8a91d795f2eef08f04a512a530c8f670819351a9..0779b2eb11c2906bd02151ae62d8d3911253805f 100755
--- a/run_agent.sh
+++ b/run_agent.sh
@@ -64,6 +64,21 @@ handle_term() {
   fi
 }
 
+set_json_log() {
+
+  if [ ! -z ${JSON_LOG} ]; then
+
+    # https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
+
+    # Replace the text line with a json formatted line.  This is ugly.
+    find . -type f -name log4j.properties \
+    -exec sed -i '/^log4j.appender.stdout.layout.ConversionPattern/c\log4j.appender.stdout.layout.ConversionPattern={"dateIso8601":"%d","timeZone":"%d{z}","priority":"%p","category":"%c","message":"%m"}%n' \
+     {} \;
+
+  fi
+
+}
+
 # Login to the API with the provided username and password and get the URL and session id
 # so we can check the status of this agent.  Read results into Bash array
 
@@ -87,6 +102,14 @@ if [ -z "$status_code" ]; then
   exit 1
 fi
 
+error_code=$(jq -nr "${json}|.error.code")
+
+if [ -z "$error_code" ]; then
+  echo "Error connecting to Informatica API: ${error_code}"
+  echo $json
+  exit 1
+fi
+
 result=($(jq -nr "${json}|.serverUrl, .icSessionId"))
 
 if [ -z "$result" ]; then
@@ -146,8 +169,9 @@ else
 
 fi
 
-# set shutdown hooks
+# set shutdown hooks and set the json log file if necessary
 prep_term
+set_json_log
 
 # run application
 ./infaagent startup
@@ -190,16 +214,6 @@ curl -sS -H "Content-Type: application/json" \
 term_child_pid=$(cat $PID_FILE)
 echoGreen "Secure Agent Starting pid:$term_child_pid"
 
-if [[ -n ${JSON_LOG+x} && "$JSON_LOG" = true ]]; then
-
-  # https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
-
-  # Replace the text line with a json formatted line.  This is ugly.
-  find . -type f -name log4j.properties \
-  -exec sed -i '/^log4j.appender.stdout.layout.ConversionPattern/c\log4j.appender.stdout.layout.ConversionPattern={"dateIso8601":"%d","timeZone":"%d{z}","priority":"%p","category":"%c","message":"%m"}%n' \
-   {} \;
-
-fi
 
 # wait until terminated
 while true