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
Try this script.
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.
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.