2

I have several scripts in a directory, each of the scripts called bot and it's number, from 1 to the number of the scripts.

What I would like to do is somehow run all of the scripts by 1 command line through the terminal (Using Ubuntu), I've used forever command to run the script without stopping and etc.

Could you make it through the terminal or using a node js script?

Is there any other commands like forever that would do it for me?

2 Answers 2

3

You could use it through the command line with the command forever.

You'll need to create a JSON file with the files you need.

Example:

[
  {
    // App1
    "uid": "app1", // ID of the script.
    "append": true,
    "watch": true,
    "script": "bot1.js", // Name of the script
    "sourceDir": "" // Where the script is located. If it's in the
                    // same location as the json file, leave it ""
  },
  {
    // App2 = > Same as app1, just different script name.
    "uid": "app2",
    "append": true,
    "watch": true,
    "script": "bot2.js",
    "sourceDir": ""
  }
]

Then you need just to run the JSON file through the forever command. Example:

forever start apps.json

You can see more information about forever here.

Sign up to request clarification or add additional context in comments.

Comments

1

My answer is the same as the answer by @Nikita Ivanov but with pm2. I personally like pm2, which also uses a config file just like forever, but it can be a js, json or yaml file.

// JS File
module.exports = {
  apps : [{
    name: "bot1",
    script: "./bot1.js",
    watch: true, // some optional param just for example
    env: {
      "NODE_ENV": "development",
    }, // some optional param just for example
    env_production : {
       "NODE_ENV": "production"
    } // some optional param just for example
  },{
    name: "bot2",
    script: "./bot2.js",
    instances: 4, // some optional param just for example
    exec_mode: "cluster" // some optional param just for example
  }]
}

Now if you do not know the number of scripts there are, it ok. Since it is JS, you can write a script to get the list of all the files in the directory and create an array similar to the one above and use that config for pm2.

module.exports = (function () {
    // logic to get all file names and create the 'apps' array
    return {
        apps: apps
    }
})()

Furthermore, you can also use the pm2 npm module and use pm2 as a module in a js script and do this.

See PM2 DOCS for more info.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.