> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ahmadraza.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Run node bend

# Running a Node.js application in the background

### Method 1: Using `nohup`

`nohup` stands for 'no hang up' and allows the process to continue running after you log out from the shell.

```sh theme={null}
nohup node index.js &
```

* `nohup`: Command to run the process immune to hangups.
* `node index.js`: Your Node.js application entry point.
* `&`: Runs the process in the background.

### Method 2: Using `screen`

`screen` is a terminal multiplexer that allows you to run multiple terminal sessions from a single SSH session.

1. Start a new screen session:

   ```sh theme={null}
   screen -S my-node-app
   ```

2. Run your Node.js application:

   ```sh theme={null}
   node index.js
   ```

3. Detach from the screen session by pressing `Ctrl + A`, then `D`.

4. To reattach to the session later:

   ```sh theme={null}
   screen -r my-node-app
   ```

### Method 3: Using `tmux`

`tmux` is another terminal multiplexer, similar to `screen`.

1. Start a new `tmux` session:

   ```sh theme={null}
   tmux new -s my-node-app
   ```

2. Run your Node.js application:

   ```sh theme={null}
   node index.js
   ```

3. Detach from the `tmux` session by pressing `Ctrl + B`, then `D`.

4. To reattach to the session later:

   ```sh theme={null}
   tmux attach -t my-node-app
   ```

### Method 4: Using `pm2`

`pm2` is a process manager specifically designed for Node.js applications.

1. Install `pm2` globally:

   ```sh theme={null}
   npm install -g pm2
   ```

2. Start your Node.js application with `pm2`:

   ```sh theme={null}
   pm2 start index.js
   ```

3. To list all running applications:

   ```sh theme={null}
   pm2 list
   ```

4. To save the process list and resurrect them on reboot:

   ```sh theme={null}
   pm2 save
   pm2 startup
   ```

This method ensures that your application will restart automatically if it crashes and can be configured to start on server reboot.

### Method 5: Using Systemd

Create a systemd service file to manage your Node.js application.

1. Create a new service file for your application:

   ```sh theme={null}
   sudo nano /etc/systemd/system/my-node-app.service
   ```

2. Add the following content to the service file:

   ```ini theme={null}
   [Unit]
   Description=My Node.js Application
   After=network.target

   [Service]
   ExecStart=/usr/bin/node /path/to/your/index.js
   Restart=always
   User=your-username
   Group=your-groupname
   Environment=PATH=/usr/bin:/usr/local/bin
   Environment=NODE_ENV=production
   WorkingDirectory=/path/to/your/application

   [Install]
   WantedBy=multi-user.target
   ```

3. Reload systemd to apply the new service:

   ```sh theme={null}
   sudo systemctl daemon-reload
   ```

4. Start your service:

   ```sh theme={null}
   sudo systemctl start my-node-app
   ```

5. Enable the service to start on boot:

   ```sh theme={null}
   sudo systemctl enable my-node-app
   ```

### Summary

Each method has its own advantages:

* `nohup` is simple and easy.
* `screen` and `tmux` are great for interactive processes.
* `pm2` offers advanced features for managing and monitoring Node.js applications.
* `systemd` integrates well with the system's init process and is robust for production environments.

Choose the method that best fits your needs and the environment where your application will run.
