To run a build file using PM2, you need to first install PM2 on your system. Then you need to create a PM2 process configuration file (sometimes called an ecosystem file) and specify the build script or command within that file. Once PM2 is installed, you can use it to run your build file.
Prerequisites - You must have PM2 already installed under your system. To install PM2, use this command:
```bash
npm install -g pm2
```
Once it is installed, follow these steps to run a build file using PM2:
1. Create a configuration file that will be used to launch your application: Navigate to the directory where your build file or script is located, and then Create a PM2 ecosystem configuration file (usually named ecosystem.config.js or pm2.config.js). You can create this file manually or use the following command to generate a sample configuration file:
```bash
pm2 init
```
2. Edit the `ecosystem.config.js` file to define your application and its settings. For example:
```javascript
module.exports = {
apps: [
{
name: "my-build",
script: "path/to/your/build/script.js", // Replace with your actual build script/command
cwd: "/path/to/your/app/directory", // Set the working directory for your script
autorestart: true, // Automatically restart the script on failure
watch: false // Watch for file changes and restart on changes (optional)
}
]
};
```
Replace `path/to/your/build/script.js` with the actual path to your build script or the command you want to run. Adjust other configuration options as needed. Behind the scenes, PM2 will detect the ecosystem.config.js, parse it, and extract the required information to launch the application [1].
Please note that in the ecosystem.config.js file, the "apps" property accepts an array of applications as its value. This enables you to oversee multiple applications using a single configuration file [1].
3. Save the ecosystem file and run the build: You need to save the `ecosystem.config.js` file for changes to take effect. After saving the file, start your build process using PM2:
```bash
pm2 start ecosystem.config.js
```
This command will start the app. You can also invoke the pm2 running processes by using the command:
```
pm2 start ecosystem.config.js && pm2 list
```
4. In case you want to stop the process when it's no longer needed, you can use:
```
pm2 stop my-build
```
Replace `"my-build"` with the name you provided in the `ecosystem.config.js` file.
Your build file is now successfully running and managed by PM2.