Node wrapper to start a terminal application like vim, emacs, tmux - node.js

TLDR; how can I run a node process from the terminal, start a process from node, exit the node process and have the process be attached to the parent terminal?
I am writing a node terminal application which should end by starting a new terminal application (e.g. vim, emacs, tmux). I want this application to run as if is was executed manually in the terminal that started the node application.
My current workaround for tmux is to run the node application, which sets up a new tmux session and echoes a tmux attach-session command just before the application exists. The user can then type this command manually in the terminal and execute it. Now the tmux session runs attached to the terminal.
I would want to move the attach command into the node application, but have the same end results. I.e. the node application terminates and the tmux session runs attached to the terminal. This seems to me to be required to do the same for applications like emacs, vim, etc. Where I cannot decouple the setup and attach. (For all I know vim and emacs can handle this decoupling, and I would be interested in knowing, but the original question asks for a general solution for any terminal application).
By attached, I mean as if the command/program was executed manually in the terminal.

The POSIX exec solves this problem as #Amadan has commented above. This solution does not work on windows.
The following snippet shows an example of how to do this with the kexec module.
const kexec = require("kexec");
kexec("emacs -nw");

Related

Where does the initial definition of a process's environment variables come from

I probably won't use the correct terminology here but here goes. I'm starting a program in tmux at boot time using crontab. Crontab runs a shell script to start tmux and then run the program. Later when I connect to the Pi everything is fine. When I connect to the tmux client though things look very different. In tmux the program is running fine but the environment is different. Up-arrow for example no longer brings up the last command and there are no colors in file displays. I figured out using printenv that the environment variables are completely different between the tmux session and the ssh session. So when tmux is run during boot it receives a completely different subset of environment variables than my ssh session when I subsequently logon.
I notice that if I create another tmux session it has the same environment as the other still running session which suggests maybe that the environment comes from the tmux server.
Can someone help me figure out how I could have tmux receive the appropriate environment variables during boot?
If I boot the os without starting tmux in crontab and then start an ssh session and then run tmux the environment variables are the same in both shells(?).
The SHELL variable in these cases is /bin/bash. In the Crontab-tmux session SHELL=/bin/sh.
I suspected the different shells (/bin/sh vs /bin/bash) had something to do with the problem. I didn't realize I could simply change the shell with a simple command. Turns out all I had to do was type /bin/bash on the command line and everything works normally.
Now I'll check to see if this works in the boot script.

running node js app in background in ubuntu

I have a nodejs compiled application that I run from a terminal window on my ubuntu vps.Is there a way to run it in the background, meaning i can afford to close my terminal window and it still works. Note this exe prints the messages on the terminal window when its running
tried PM2, it errors out saying "awaiting restart"
tried nohup, it does not error out and shows process has started but exe does not what it shall do, meaning its not working.
if i do ./app , it works but then i can not close the terminal window.
used tmux, loving it so far.
think might use PM2 with tmux where i can.

What can I do to make node/npm/related commands send their output back to the terminal I used to launch them?

What can I do to make node/npm/related commands send their output back to the terminal I used to launch them?
I've recently installed NodeJS 10.4.2 on Windows 10. When I run any node or npm command from PowerShell, Git Bash, or the Node terminal shortcut, a new window is opened, the output is directed there, and the window is closed. I feel like there must be a setting I've flipped under a previous installation, but I can't find it, and it's making me bananas.

How to clear node.js terminal in Cent OS 7

I'm using Node.js in VScode. Say my run command is node index.js. As I run command various console.log statements print during course now I want to clear console log without stopping current node process. In existing case To clear the console I have to stop the running process using Ctrl+c. use clear command in terminal to clear screen and again run the process using node index.js. My question is there any method by using which i can clear terminal window without stopping current node process.
You can use console.clear() function in your node program. If you want to clear vscode terminal window, just click by right mouse on terminal and select Clear (shortcut Ctrl + K)

Using script to automatically start program when the system boot up (linux, shell)

Here is the situation, I'm planning to use a simple script to start a program call "STAF", when the Suse system is fully booted. I have achieved this by putting it in the "/etc/init.d/", but this script is basically executed at the background, which means that I cannot see its progress.
When the "STAF" is started this way it works but it doesn't show any working progress when its running service (for example ping, or system backup), instead if I start the "STAF" manually by running the same script whit a terminal, the working progress of "STAF" can be seen on the terminal. Its sort of like the program needs to be started with a interactive terminal, but how can I make this starting process automatic and it should imitate human opening a terminal and run the script?
Sorry if I explained it poorly because its a confusing situation. Thanks.
First, go to the KDE Startup and Shutdown options under System Settings. Then add this command as a new startup script:
konsole -e bash nameofyourscript.sh
I believe the screen utility can do what you describe. Instead of running STAF on startup, you would run screen STAF. To open that terminal, you would run screen -ls to get the screen ID, and screen -r ... to open it.
(Disclaimer: I have not tried this.)

Resources