running node js app in background in ubuntu - node.js

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.

Related

How do I keep my discord bot online even with my terminal closed

I have a pretty simple discord.js bot and would like to keep it running even when I close my terminal and/or my computer.
I have already tried pm2 and just keeping my terminal open throughout the day, but it wastes battery.
I keep running pm2 run NAMEOFFILE.JS in the right folder, but it already says I'm running the file.
You must use a process manager, like pm2 (pm2 start yourfile.js) or forever (forever start yourfile.js). You can install them with npm (npm i -g pm2, npm i -g forever).
EDIT: Yeah use PM2 or some other application, screen is a bad idea.
If you're on either MacOS or Linux, you can use the screen command (Or cygwin on Windows). It basically creates another terminal. To use it you can do screen -R <name>.
When you use the -R <name> (capital) the computer will look for a screen with the name and reattach to it. If it can't find a screen with that name it will create a new screen with that name.
So for you, you might do screen -R Discord. After you attach to the screen you can do whatever you want as if it were a terminal. When you're done, you can press ctrl+a then d to detach from the screen, then you can close the terminal window.
https://ss64.com/osx/screen.html

How to access a running nodemon script after closing and reopening the terminal?

I'm running a node.js script using nodemon, but I'm wondering if it's possible to reaccess the nodemon process using the terminal after closing it.
Normally I would type "npm run start" that points to a "nodemon index.js" in the package.json, and some yellow and green logs would appear to tell the state of the process(starting, restarting). But if I close the terminal, how can I access this process again, without having to type another "npm run start"?
Just the case nodemon is not suitable for that, I would like to know which tools could help me achieve that, such as pm2, etc.
Open a screen or tmux session, and then run your app.
This permits the terminal to be disconnected from the program and reconnected later by reviving the same session.
cntr+j leaves your server running in the background, then if you work on the code and saves it and the server crashes because the server is still running.
A solution is just to minimize the terminal and not to close it, or opening via command prompt.

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)

Node wrapper to start a terminal application like vim, emacs, tmux

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");

Can to keep web app running even when the console closed?

I run "gradle tomcatRunWar" to start a web app running. When the console is closed, it will stop. How to keep the web app running even when console closed? I prefer not to use screen, and so on...
If you prefer not to use a multiplexer like screen or tmux, you can use nohup as a simple one-liner.
The following command will run gradle in the background:
nohup gradle tomcatRunWar &
Here is some more info on the nohup command.

Resources