Skip to main content
Glama

ensure_service

Control service states on remote servers via SSH by starting, stopping, enabling, or disabling services to maintain desired configurations.

Instructions

Ensures a service is in the desired state

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sessionIdYesSSH session ID
nameYesService name
stateYesDesired state

Implementation Reference

  • The implementation of the ensureService tool handler.
    export async function ensureService(
      sessionId: string,
      serviceName: string,
      state: 'started' | 'stopped' | 'restarted' | 'enabled' | 'disabled',
      sudoPassword?: string
    ): Promise<ServiceResult> {
      logger.debug('Ensuring service state', { sessionId, serviceName, state });
    
      const session = sessionManager.getSession(sessionId);
      if (!session) {
        throw new Error(`Session ${sessionId} not found or expired`);
      }
    
      try {
        // Detect init system
        const osInfo = await sessionManager.getOSInfo(sessionId);
        const initSystem = osInfo.init;
    
        if (initSystem === 'launchd') {
          throw createSudoError(
            'launchd services are not managed by this tool',
            'Use launchctl directly on macOS hosts'
          );
        }
    
        if (initSystem === 'windows-service') {
          throw createSudoError(
            'Windows services are not managed by this tool',
            'Use sc.exe or PowerShell to manage Windows services'
          );
        }
    
        if (initSystem === 'unknown') {
          throw createSudoError(
            'No supported init system found',
            'Supported init systems: systemd, service'
          );
        }
    
        logger.debug('Detected init system', { sessionId, initSystem });
    
        let command: string;
    
        if (initSystem === 'systemd') {
          switch (state) {
            case 'started':
              command = `systemctl start ${serviceName}`;
              break;
            case 'stopped':
              command = `systemctl stop ${serviceName}`;
              break;
            case 'restarted':
              command = `systemctl restart ${serviceName}`;
              break;
            case 'enabled':
              command = `systemctl enable ${serviceName}`;
              break;
            case 'disabled':
              command = `systemctl disable ${serviceName}`;
              break;
          }
        } else {
          // Traditional service command
          switch (state) {
            case 'started':
              command = `service ${serviceName} start`;
              break;
            case 'stopped':
              command = `service ${serviceName} stop`;
              break;
            case 'restarted':
              command = `service ${serviceName} restart`;
              break;
            case 'enabled':
              command = `chkconfig ${serviceName} on || update-rc.d ${serviceName} enable`;
              break;
            case 'disabled':
              command = `chkconfig ${serviceName} off || update-rc.d ${serviceName} disable`;
              break;
          }
        }
    
        logger.debug('Executing service command', { sessionId, serviceName, command });
    
        const result = await execSudo(sessionId, command, sudoPassword);
    
        const serviceResult: ServiceResult = {
          ok: result.code === 0
        };
    
        if (result.code === 0) {
          logger.info('Service state changed successfully', { sessionId, serviceName, state });
        } else {
          logger.error('Service state change failed', {
            sessionId,
            serviceName,
            state,
            code: result.code,
            stderr: result.stderr
          });
        }
    
        return serviceResult;
    
      } catch (error) {
        logger.error('Failed to ensure service state', { sessionId, serviceName, state, error });
        throw error;
      }
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/oaslananka/mcp-ssh-tool'

If you have feedback or need assistance with the MCP directory API, please join our Discord server