AWS EC2: keep running node.js after exit console - node.js

I use aws ec2 to host web server with node.js and apache.
So far, I always need to login ec2 through terminal to run npm start.
I wanna make it keep running even if Im not on terminal. How am I supposed to setup for it?
I turned on https, but nothing happened.
I appreciate it in advance.

One option is using the screen command:
screen
You will reach a new bash prompt for that screen. Run your app.
To detach the screen, press Ctrl+A, then D to detach from that screen.
To reattach screen when you next SSH in, use
screen -ls
to see detached screens and use
screen -r xxxxx
with the screen number to reattach that screen.

Related

Running python script in background EC2

I have a code.py script that I want to run on my Linux EC2 instance. I want it to run even when the tab of the EC2 instance in my browser is closed, so that the code is running in the background.
Is there any way to do this?
this command run your python file on another screen that called YourScreenName.
screen -dmS YourScreenName python3 code.py
if you close EC2 instance tab on your browser, this screen will be running on your server.
for access to this screen you can use this command.
screen -r YourScreenName
and for quit from this screen use Ctrl + a + d

How to use tmux inside release pipeline

I have an azure release pipeline to run my backend application on a DigitalOcean server.
I would like to use a tmux window so I can still see the terminal in case I need to debug the production backend.
tmux new-session -A -s tmuxWindowName
pkill java
mv backend/demo-0.0.1-SNAPSHOT.jar backend/backend.jar
java -Xmx800M -jar backend/backend.jar
tmux detach
but this doesn't work because the azure ssh connection is not attached to a terminal. I get the following error:
##[error]open terminal failed: not a terminal
I tried setting the term with "TERM=xterm" and googling this issue comes up with lots of people that are able to manipulate the SSH connection command but I can't since I use an SSH service connection.
What to do?
You can create a detached tmux session by adding -d to new-session which will not require a terminal (until you attach it which will be presumably from somewhere else).

Keeping ExpressJS Application Running After Terminal is Closed

My application, working on server, but then I close the terminal window and your application stops. How can I solve this issue.
How to make a application run permanently in background ?
Is there any Express plugins for solving this issues
You could found answer from ExpressJs official guide about Process managers for Express apps.
PM2 and Forever is recommended by official document for keeping your application running background and automatically restarts.
You can use shell screen:screen commands
Install screen : sudo apt install screen
Create a screen : Ctrl-a c
Go to your app folder and run app: node server.js (check any nodes api tp confirm your app is working)
Detach screen : Ctrl-a d
List screens to check your app is running: screen -ls
Close terminal.
PS: To attach the screen your app is running
List available screens : screen -ls
Attach screen : screen -r {pid}
Kill a screen using : screen -X -S {pid} quit

Tmux - How can I run node app forever after detaching tmux.?

I am making my first app in node js and i uploaded it on server. I have a terminal where I am running a session containing two panes, one for running mongod and second for running node app.js . Now both of them run as long as I keep it running. But how can we make sure after quitting the window it keeps running both of them mongod and node. I am using tmux, mongodb, express and node.
I tried tmux detach and tmux attach. They work. But for tmux detach first i have to quit the current command by ctrl + c and then i would be able to run tmux detach. Am I doing something wrong? Please Help
I presume this is for a development environment and not production. In production, keeping something running "forever" makes using a process supervisor to make sure it is restarted when it crashes.
In development, you can use <Prefix>-d to detach without cancelling the current command. The Prefix for Tmux is Control-B by default.

Start a server running with screen in a vagrant box

I'm trying to run a server automatically when my vagrant box boots.
Similar to start screen detached in a vagrant box with ssh, how?, except I'm trying to do it with a provisioning script set to run: "always".
I'm doing something like this: nohup screen -S server -mL -d bash -c 'start-my-server.sh'.
The server starts fine, and if I would have done this within the shell, I could switch to the server with screen -r server.
When I go in after with vagrant ssh, it doesn't find any screens...I'm assuming this is because its not the same shell session.
Is there anyway to get a hold of that screen session?
Edit
Forgot to mention that I had prefixed the screen command with nohup
The answer is that vagrant provision was running as a privileged user, and therefore I couldn't see the screen logging in as the vagrant user.

Resources