How to start node service after restart server

How to start node service after restart server || manoj damor
To ensure that your Node.js service automatically starts after a system restart , you can use various methods depending on your operating system and setup. Below are the most common approaches:
How to Install Node.js on an Ubuntu 22 Server
Method 1: Using pm2
pm2
is a popular process manager for Node.js applications. It helps you manage and keep your applications alive indefinitely. It also supports automatic restarts and can be set up to launch your service after a system reboot.
Step 1: Install pm2
First, install pm2
globally using npm:
npm install -g pm2
Step 2: Start Your Node.js Application
Start your Node.js application using pm2
:
pm2 start app.js --name "my-node-app"
Here, replace app.js
with the entry file of your Node.js project.
Step 3: Save the Process List
To save the list of running processes, so they are automatically restarted on reboot, run:
pm2 save
Step 4: Set Up Startup Script
Generate and configure the startup script to ensure pm2
starts on boot:
pm2 startup
This command will output a command that you need to run with superuser privileges. Run the provided command to complete the setup.
Method 2: Using Systemd (Linux)
If you’re running your Node.js application on a Linux server, you can create a systemd
service to manage your application.
Step 1: Create a Systemd Service File
Create a service file for your Node.js application:
sudo nano /etc/systemd/system/my-node-app.service
Add the following content to the file:
[Unit]
Description=My Node.js Application
After=network.target
[Service]
ExecStart=/usr/bin/node /path/to/your/app.js
Restart=always
User=your-username
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/path/to/your/app
[Install]
WantedBy=multi-user.target
Replace /path/to/your/app.js
with the actual path to your Node.js application. Make sure the User
is set to your username and the paths are correct.
Step 2: Reload Systemd and Start the Service
Reload systemd
to apply the changes:
sudo systemctl daemon-reload
Start your service:
sudo systemctl start my-node-app
Enable the service to start on boot:
sudo systemctl enable my-node-app
Step 3: Managing the Service
You can manage your Node.js service using the following commands:
- Check Status:
sudo systemctl status my-node-app
- Stop the Service:
sudo systemctl stop my-node-app
- Restart the Service:
sudo systemctl restart my-node-app
Method 3: Using Forever (Alternative to PM2)
forever
is another process manager for Node.js that allows you to keep your script running continuously.
Step 1: Install Forever
Install forever
globally:
npm install -g forever
Step 2: Start Your Application with Forever
Start your Node.js application:
forever start app.js
To list all running forever processes:
forever list
To stop the process:
forever stop app.js
Step 3: Auto-Start Forever on Boot
To make forever
start your Node.js app on boot, you can add the startup command to your server’s init system. For example, add the command to your crontab:
crontab -e
Add the following line to start your Node.js app on reboot:
@reboot /usr/local/bin/forever start /path/to/your/app.js
Conclusion
You can choose from pm2
, systemd
, or forever
to manage your Node.js services and ensure they start after a system reboot or project restart. Each tool has its advantages:
pm2
: Offers a powerful and easy-to-use solution with many features.systemd
: Ideal for Linux servers, providing a native solution for service management.forever
: Simpler alternative topm2
, suitable for basic use cases.
Pick the one that best fits your needs and environment.