skip to Main Content

I have the following code that should open a terminal and run a command:

import { ChildProcess, spawn, ChildProcessWithoutNullStreams, ChildProcessByStdio } from 'child_process';
import { Writable, Readable } from 'stream';

const terminal: 
ChildProcessWithoutNullStreams | 
ChildProcessByStdio<Writable, Readable, Readable> |
ChildProcess = spawn('open', ['-b', "com.googlecode.iterm2"]);

terminal.on('spawn', () => {
  console.log("spawn_0")
  if (terminal.stdin) {
    console.log("spawn_1")
    terminal.stdin.write(`tail -n +1 -f ${cacheDir}/${nameFile}.txtn`);
    terminal.stdin.end();
  }
});

terminal.on('open', () => {
  console.log("open_0")
  if (terminal.stdin) {
    console.log("open_1")
    terminal.stdin.write(`tail -n +1 -f ${cacheDir}/${nameFile}.txtn`);
    terminal.stdin.end();
  }
});

currently when this code is run, this is printed in the console:

spawn_0
spawn_1

The terminal opens correctly, but the "ls" command does not execute.

Info:

I have a txt file, which is continuously updated.
I have to make sure that when the user calls this part of code, the terminal chosen by the user is started, redirecting the file output to the terminal.

2

Answers


  1. Try this script.

    import { spawn } from 'child_process';
    
    const terminal = spawn('open', ['-b', 'com.googlecode.iterm2']);
    
    terminal.on('exit', (code, signal) => {
      console.log(`Child process exited with code ${code} and signal ${signal}`);
    });
    
    terminal.on('error', (err) => {
      console.error(`Error: ${err.message}`);
    });
    
    terminal.stdout?.on('data', (data) => {
      console.log(`stdout: ${data}`);
    });
    
    terminal.stderr?.on('data', (data) => {
      console.error(`stderr: ${data}`);
    });
    
    terminal.on('close', (code, signal) => {
      console.log(`Child process closed with code ${code} and signal ${signal}`);
    });
    
    // Wait for iTerm2 to open before sending the command
    setTimeout(() => {
      terminal.stdin?.write('lsn');
      terminal.stdin?.end();
    }, 2000);
    
    Login or Signup to reply.
  2. Thank you for the clarification. If you want to redirect the output of a continuously updated text file to the terminal, and you’ve specified that ls is a placeholder for the actual command you want to execute, and considering the challenges you’ve mentioned, let’s try this code.

    import { spawn } from 'child_process';
    import * as fs from 'fs';
    
    const terminal = spawn('open', ['-b', 'com.googlecode.iterm2']);
    
    terminal.on('exit', (code, signal) => {
      console.log(`Child process exited with code ${code} and signal ${signal}`);
    });
    
    terminal.on('error', (err) => {
      console.error(`Error: ${err.message}`);
    });
    
    terminal.stdout?.on('data', (data) => {
      console.log(`stdout: ${data}`);
    });
    
    terminal.stderr?.on('data', (data) => {
      console.error(`stderr: ${data}`);
    });
    
    // Replace 'ls' with your actual command
    const command = 'tail -n +1 -f /path/to/your/file.txt';
    
    // Wait for iTerm2 to open before sending the command
    setTimeout(() => {
      terminal.stdin?.write(`${command}n`);
      terminal.stdin?.end();
    }, 2000);
    

    Here, I’ve replaced ls with a placeholder command (tail -n +1 -f /path/to/your/file.txt) that continuously outputs the contents of a text file. Make sure to replace /path/to/your/file.txt with the actual path to your continuously updated text file.

    This modified code should open iTerm2, wait for 2 seconds, and then send the specified command to continuously display the contents of the text file in the terminal.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search