Q:

How do I run all the commands in a bash script?

I am writing a bash script that executes a series of javascript files, all of which are some kind of MySQL queries. The thing is, when the first line of the script executes, it stops the process and does not let the other javascript files to run.
I have already tried using & after each line hoping it would run in the background and not stop the flow of execution.
#!/bin/bash
# a script to delete the database and create the tables
node createDatabase &
node createTables &

When the createDatabase.js file executes properly, there is a callback function which outputs "database created successfully" and the execution just freezes at that.

A:

The issue must be from nodejs, Nodejs does not terminate the process on DB creation, you have to call process.exit(0) on success or process.exit(1) on error otherwise it will wait for the signal to terminate or stop.
Here is a simple Nodejs script that creates a DataBase and exit base on the response from DB.
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "my.test.com",
  user: "root",
  password: "password"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  con.query("CREATE DATABASE mydbtest", function (err, result) {
    if (err){
      throw err;
      process.exit(1);
    }
    else{
      console.log("Database created");
      process.exit(0);

    } 

  });
});

Now another script that will make tables on the above DB.
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "mydb.test.com",
  user: "root",
  password: "password",
  database: "mydbtest"
});

con.connect(function(err) {
  if (err){
    throw err;
    process.exit(1);
  }
  else{
    console.log("Connected!");
  var sql = "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))";
  con.query(sql, function (err, result) {
    if (err){
      throw err;
      process.exit(1)
    } 
    else{
      console.log("Table created");
process.exit(0)
    }
  });
  } 

});

The important section in this code is process.exit(0) or process.exit(1).
Now the Bash script will be
#!/bin/bash
echo "creating db"
nodejs creatdb.js
echo "creating talbe"
nodejs createtable.js

You do not need to send them in the background and now on base one exit code you can exit the script or take any sort of decision.

