{"record":{"id":"1b184ce9f3a0e2ee","repo":"pentaho/pentaho-kettle","slug":"failed-to-execute-command-command","errorCode":null,"errorMessage":"Failed to execute command: ${command}","messagePattern":"Failed to execute command: (.+?)","errorType":"exception","errorClass":"SshConnectionException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/core/ssh/mina/MinaSshConnection.java","lineNumber":472,"sourceCode":"            Thread.sleep( 100 );\n            ex = ch.getExitStatus();\n          }\n          exit = ex == null ? -1 : ex;\n        } else {\n          // Timeout occurred\n          throw new SshTimeoutException( \"Command execution timed out after \" + timeoutMs + \"ms\" );\n        }\n      }\n      String outStr = stdout.toString( StandardCharsets.UTF_8 );\n      String errStr = stderr.toString( StandardCharsets.UTF_8 );\n      return new ExecResult( outStr, errStr, outStr + errStr, exit, exit != 0 );\n    } catch ( SshTimeoutException e ) {\n      throw e; // Re-throw timeout exceptions as-is\n    } catch ( InterruptedException e ) {\n      Thread.currentThread().interrupt(); // Restore interrupted status\n      throw new SshConnectionException( \"Command execution was interrupted\", e );\n    } catch ( Exception e ) {\n      throw new SshConnectionException( \"Failed to execute command: \" + command, e );\n    }\n  }\n\n  @Override\n  public ExecResult exec( String command ) throws SshConnectionException {\n    return exec( command, config.getCommandTimeoutMillis() );\n  }\n\n  @Override\n  public SftpSession openSftp() throws SshConnectionException {\n    try {\n      var factory = SftpClientFactory.instance();\n      var sftp = factory.createSftpClient( session );\n      return new MinaSftpSession( sftp );\n    } catch ( Exception e ) {\n      throw new SftpException( \"Failed to open SFTP session\", e );\n    }\n  }","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/core/ssh/mina/MinaSshConnection.java#L454-L490","documentation":"The generic catch-all in exec converts any unexpected Exception (channel open failure, IO error while streaming output, session closed, etc.) into SshConnectionException('Failed to execute command: <command>') with the original exception attached. It means command execution failed for a reason not classified as timeout or interruption.","triggerScenarios":"Any non-timeout exception inside exec: channel could not be opened, session already disconnected/closed, IOException reading stdout/stderr streams, or command string rejected by the session.","commonSituations":"Calling exec after the connection was closed or dropped; session timed out server-side (ClientAliveInterval) before the command ran; command too long or containing characters the shell mishandles.","solutions":["Check the cause attached to the exception for the true failure reason","Ensure the connection is still open ( isConnected() ) before calling exec; reconnect if needed","Increase server ClientAliveInterval / client heartbeat to keep long sessions alive","Validate/quote the command string (avoid unescaped special characters)","Reconnect and retry once on transient session-closed causes"],"exampleFix":"// before\nif ( !conn.isConnected() ) { /* proceed anyway */ }\nExecResult r = conn.exec( cmd );\n// after\nif ( !conn.isConnected() ) { conn.connect(); conn.authenticate(); } // re-establish session\nExecResult r = conn.exec( cmd );","handlingStrategy":"try-catch","validationCode":"// guard before exec\nif ( conn == null || !conn.isConnected() ) {\n  throw new IllegalStateException( \"SSH session not connected; call connect() first\" );\n}\nif ( command == null || command.trim().isEmpty() ) {\n  throw new IllegalArgumentException( \"Command must be non-empty\" );\n}","typeGuard":null,"tryCatchPattern":"try { result = conn.exec( cmd ); }\ncatch ( SshConnectionException e ) {\n  if ( e.getMessage().startsWith( \"Failed to execute command\" ) && e.getCause() != null ) {\n    log.error( \"exec of '{}' failed: {}\", cmd, e.getCause().toString() );\n    reconnectAndRetryOnce( conn, cmd ); // transient session-closed handling\n  } else throw e;\n}","preventionTips":["Always check isConnected() before exec; reconnect on failure","Configure heartbeats to keep sessions alive during long waits","Quote/escape shell metacharacters in command strings","Inspect the attached cause — the generic message hides the real reason"],"tags":["ssh","command-execution","session"],"backgroundTag":"network-request-failed","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}