I have a nodejs to run on Ubuntu. However, i'm using Putty (i'm a windows user) to connect to the server.
The apps runs fine, the only problem that i have that as soon as i close Putty (Session), so does the nodejs stop running.
Is there any specific command should i run instead 'node server.js' ? Any help is appreciated
Thanks
CodePudding user response:
You can handle this situation very efficiently by installing pm2.
PM2 is a daemon process manager that will help you manage and keep your application online 24/7
Installation
The latest PM2 version is installable with NPM or Yarn:
$ npm install pm2@latest -g
// or
$ yarn global add pm2
Start an app in background
The simplest way to start, daemonize and monitor your application is by using this command line:
$ pm2 start app.js
Once you've done this, you can simply close your putty session and your NodeJS server will keep running as a daemon in the background.
CodePudding user response:
Start your app as a service.
For that create a systemd unit file under /lib/systemd/system/my-app.service
with the following content:
[Unit]
Description=my-app description
After=network-online.target
[Service]
ExecStart=/usr/bin/node /opt/.../index.js
Restart=always
#User=nobody
# Note Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody'
# Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/opt/.../
[Install]
WantedBy=multi-user.target
Then tell systemd to detect the new service systemctl daemon-reload
.
After that enable & start your service:
systemctl enable my-app
systemctl start my-app
Then it starts automatically after a reboot & keeps running if you logout.