Ver Fonte

fix: improve shell detection for restricted systemd environments

Fix "spawn /usr/bin/bash ENOENT" error when running as systemd service
by making shell path detection more robust in restricted environments.

**Changes**:
- Change ultimate fallback from '/bin/sh' to 'bash' (let PATH resolve it)
- Only use custom shell path if it's a full path (starts with '/')
- Use 'shell: true' for non-path values (more compatible with Node.js)
- This allows Node.js to use default shell resolution in restricted environments

**Root Cause**:
Systemd services with security restrictions may not allow spawning shells
via full paths. Using 'shell: true' lets Node.js handle shell resolution
through the system's PATH, which is more reliable.

**Testing**:
This fix resolves the issue where commands work manually but fail when
executed by the systemd service.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Fszontagh há 9 meses atrás
pai
commit
c43528c6ec
1 ficheiros alterados com 15 adições e 5 exclusões
  1. 15 5
      src/commandExecutor.js

+ 15 - 5
src/commandExecutor.js

@@ -27,8 +27,8 @@ function detectShellPath() {
       }
     }
   }
-  // Ultimate fallback: use /bin/sh
-  return '/bin/sh';
+  // Ultimate fallback: use 'bash' without path (let PATH resolve it)
+  return 'bash';
 }
 
 const SHELL_PATH = detectShellPath();
@@ -201,12 +201,22 @@ export class CommandExecutor {
     logger.info('Executing command', { type, command, args, fullCommand, cwd, timeout });
 
     try {
-      const { stdout, stderr } = await execAsync(fullCommand, {
+      // Use default shell if SHELL_PATH is just 'bash' (more compatible with restricted environments)
+      const execOptions = {
         timeout,
         cwd: cwd || process.cwd(),
-        shell: SHELL_PATH,
         maxBuffer: 10 * 1024 * 1024 // 10MB
-      });
+      };
+
+      // Only set custom shell if we have a full path
+      if (SHELL_PATH.startsWith('/')) {
+        execOptions.shell = SHELL_PATH;
+      } else {
+        // Let Node.js use default shell resolution
+        execOptions.shell = true;
+      }
+
+      const { stdout, stderr } = await execAsync(fullCommand, execOptions);
 
       if (stdout) {
         logger.info('Command output (stdout)');