Create a .service file for your Node.js program:

[Unit]

Description=My Node.js program

After=network.target


[Service]

ExecStart=/usr/bin/node /path/to/your/node/program.js

Restart=always

User=yourusername

Group=yourgroupname

Environment=NODE_ENV=production


[Install]

WantedBy=multi-user.target



2.Copy the .service file to the /etc/systemd/system directory:

sudo cp your-program.service /etc/systemd/system/

3.Reload the systemd daemon:

sudo systemctl daemon-reload

4.Enable the service to start on boot:

sudo systemctl enable your-program.service

5. Start the service

sudo systemctl start your-program.service





///////////////////// pm2 /////////////////////////////


pm2 is a process manager for Node.js applications that allows you to keep your Node.js applications running in the background, restart them automatically if they crash, and monitor their resource usage. Here are the basic steps to use pm2 in Linux:


Install pm2 globally using npm:


Copy code

npm install pm2 -g

Start your Node.js application with pm2:


sql

Copy code

pm2 start app.js

Replace app.js with the path to your Node.js application.


//Check the status of your application:



pm2 status

//To stop your application, use the stop command:



pm2 stop app

//Replace app with the name or ID of your application.


//To start your application again, use the start command:



pm2 start app

//Replace app with the name or ID of your application.


//To monitor your application's resource usage, use the monit command:



pm2 monit

//To automatically start your application when the system boots up, use the startup command:


pm2 startup

//This will generate a command that you need to run with superuser privileges to set up pm2 as a service.


//Save the current list of running applications to automatically start them on system boot:



pm2 save