Aura
Aura

Reputation: 317

PM2 spams shells/terminal on start

So I'm using PM2 to manage my discord bots from a web interface and there is a button that starts all the bots.

What Is the problem?
Every time the function is run(or the button is clicked), a lot of shells spam open and close instantly on my screen.

What does the function do?
The function first gets all the discord bot directories from the main directory, then loops through them to find a package.json in each directory, using the main entry file from package.json to start the app.

The function:


getDirectories(`./${process.env.SECRET_PATH}`, function (Data) {
    Data.forEach(Folder => {
        if (Folder.toLowerCase() != "logs") {
            if (!fs.existsSync(`./${process.env.SECRET_PATH}/${Folder}/node_modules`)) {
                fs.mkdir(`./${process.env.SECRET_PATH}/${Folder}/node_modules`, function () {
                    Terminal(`cd ./${process.env.SECRET_PATH}/${Folder} && npm install`).then(data => {

                    }).catch(err => {

                    })
                })
            }
            PackageFile = `./${process.env.SECRET_PATH}/${Folder}/package.json`;
            if (fs.existsSync(PackageFile)) {
                fs.readFile(PackageFile, 'utf8', function (err, data) {
                    if (err) { console.error(err); }
                    Package = JSON.parse(data);
                    console.log(Package)
                    setTimeout(() => {
                        pm2.start({
                            name: Folder,
                            detached: true,
                            script: `./${process.env.SECRET_PATH}/${Folder}/${Package.main}`,
                            out_file: `./${process.env.SECRET_PATH}/logs/${Folder}.strout.log`,
                            error_file: `./CanorusNecked/logs/${Folder}.strerr.log`,
                        }, function (err, apps) {
                            if (err) { console.log(err) };
                        })
                    }, 250);
                })
            }
        }
    });
})

The opening shells: enter image description here

Upvotes: 0

Views: 1208

Answers (1)

Aura
Aura

Reputation: 317

I fixed the error by adding these values in the first parameter of the pm2.start() function.

{
    detached: true,
    max_restarts: 5,
    min_uptime: 5000,
    watch_delay: 5000,
    autorestart: false,
    restart_delay: 1000,
    exp_backoff_restart_delay: 100,
}

Upvotes: 0

Related Questions