Skip to main content

Process

The Process module provides a comprehensive set of APIs for process management and monitoring. It is accessible through the Process class.

Overview

The Process module offers a unified interface for:

  • Process creation and execution
  • Process status monitoring
  • Process termination
  • Process log streaming

Features

  • Process creation and execution with command and arguments
  • Real-time process status monitoring
  • Process termination and cleanup
  • Process log streaming with event-based architecture
  • Memory usage tracking
  • Cross-platform process management
  • Event-driven process state changes
  • One-time event listeners

Examples

Creating and Executing Processes

// Execute a command
const process = await Process.exec('command', ['arg1', 'arg2']);

// Get process ID
const processId = process.id;

Process Information

// Get information about a specific process
const info = await Process.info('process-id');

// Get information about the current process
const currentInfo = await process.info();

// List all running processes
const processes = await Process.list();

Process Control

// Kill a specific process
await Process.kill('process-id');

// Kill the current process
await process.kill();

Process Events

The Process module supports event-driven process monitoring through the ProcessEvent enum:

enum ProcessEvent {
Running = "processRunning",
Killed = "processKilled",
Exited = "processExited"
}

Event Handling

// Bind to process events
process.bind(ProcessEvent.Running, () => {
console.log('Process is running');
});

process.bind(ProcessEvent.Killed, () => {
console.log('Process was killed');
});

process.bind(ProcessEvent.Exited, () => {
console.log('Process has exited');
});

// One-time event listener
process.once(ProcessEvent.Exited, () => {
console.log('Process exited (one-time event)');
});

// Unbind event
process.unbind(ProcessEvent.Running);

Process Logs

The LogEventWatcher class provides real-time access to process logs:

// Get log event watcher
const logWatcher = process.logEvents();

// Listen to log events
logWatcher.bind(LogEvent.Data, (data) => {
console.log('Log data:', data);
});

logWatcher.bind(LogEvent.Error, (error) => {
console.error('Log error:', error);
});

logWatcher.bind(LogEvent.Out, (data) => {
console.log('Standard output:', data);
});

logWatcher.bind(LogEvent.End, () => {
console.log('Log stream ended');
});

// Close the log watcher when done
logWatcher.close();
note

Each time you get the process logs, you will receive the latest logs. The system does not store historical logs. If you need to capture all logs, you should start listening to the data event from the moment the process starts.

Best Practices

  1. Process Management

    • Always clean up processes when they are no longer needed
    • Monitor process resources to prevent memory leaks
    • Implement proper error handling for process operations
  2. Event Handling

    • Remove event listeners when they are no longer needed using unbind
    • Use one-time event listeners for events that should only trigger once
    • Handle all possible process states
  3. Log Management

    • Close log watchers when they are no longer needed
    • Start listening to logs as early as possible to capture all output
  4. Security

    • Validate and sanitize command arguments