How to run nodejs in the background on Linux

PHPz
Release: 2023-04-05 10:33:37
Original
1575 people have browsed it

With the popularity of server-side JavaScript like nodejs and the growth of web applications, more and more developers need to build and publish such applications. These applications need to be able to run in the background, in this article we will explore some methods on how to run nodejs in the background on a Linux server.

  1. Using Process Manager PM2

PM2 is a popular process manager and is the first choice for running nodejs in the background in a production environment. It provides many features and options, including process monitoring, automatic restart, load balancing, and more. After using PM2, we can easily run nodejs applications in the background.

Installing PM2

To use PM2, we need to install it first. Enter the following command in the terminal:

npm install -g pm2
Copy after login

Start the nodejs application

When starting the application, using PM2 is very simple, just enter the following command in the terminal:

pm2 start app.js
Copy after login

The app.js here is the entry file of our nodejs application. PM2 will automatically create a background process to run this file.

PM2 also provides many useful commands that we can use to manage the nodejs process. The following are some of the most commonly used commands:

pm2 list              # 列出所有进程
pm2 stop <app-name>   # 停止指定应用程序
pm2 restart <app-name># 重启指定应用程序
pm2 delete <app-name> # 删除指定应用程序
Copy after login
  1. Using nohup

nohup is a tool for starting background processes under Linux systems. It allows us to run a command under the terminal and transfer it to the background after the terminal is closed. After using nohup, our nodejs application can still run in the background even if we have exited the terminal.

Use nohup to run nodejs in the background

To use nohup to run nodejs in the background, just enter the following command in the terminal:

nohup node app.js &
Copy after login

The app.js here is our nodejs application entry file. The "&" symbol means transferring the command to the background for execution.

End running

When we need to stop a nodejs application running in the background, we need to first use the ps command to find the process ID of the application, and then use the kill command to end the process:

ps -ef | grep node     # 找到进程 ID
kill <pid>             # 结束进程
Copy after login
  1. Using screen

screen is a tool that runs commands on a separate terminal screen. We can run any command in it including nodejs applications. After using screen, our nodejs application can still run on the screen even if we exit the terminal.

Install screen

If screen is not installed on your server, you need to install it first. Enter the following command in the terminal:

sudo apt-get update
sudo apt-get install screen
Copy after login

Start screen

To start a screen terminal, we can use the following command:

screen -S <screen-name>
Copy after login

The screen-name here is what we want The name by which the terminal is named.

Running nodejs in screen

Now we can run the nodejs application on the launched screen terminal. Enter the following command in the terminal:

node app.js
Copy after login

When we exit the screen terminal, the application will still be running in the background.

Reconnect to the screen terminal

If you need to reconnect to the screen terminal, just use the following command:

screen -r <screen-name>
Copy after login

The screen-name here is what we named the terminal name.

End running

When we need to stop the nodejs application running in the background, we need to first use the Ctrl C command in the screen terminal to stop the nodejs process, and then use the following command to close the screen terminal:

exit
Copy after login

Summary

On a Linux server, there are many ways to run nodejs applications in the background. PM2 is one of the most commonly used process managers, other methods include using nohup and screen. Whichever method you choose, you need to ensure that your application can run securely in the background and that you can manage and monitor it effectively.

The above is the detailed content of How to run nodejs in the background on Linux. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template