1

I am a total beginner in shell scripting.

I have a node js program, and an API, which uses mongoDB. So the API uses express and is linked to my routes and schemas - all I have to do in order to start the API server is run node app.js

However, I also have my main program, which polls specific urls that the user adds through the database, and then saves the data that is returned from the poll request in the database. This happens every 'x' seconds, therefore I always open the mongo connection when the poll happens, and close it as soon as it finishes it. So in order to run this program, I need to run node main.js

The app.js seems to just open the mongo connection once, when the program is run.

So my question is - can I link them together somehow by writing a bash script so I can start both of the processes together and end them when needed?

I tried doing this:

#!/usr/bin/bash
# declare STRING variable
STRING="Starting node processes"
#print variable on a screen
echo $STRING

node misrepo/app.js
node misrepo/main.js

However this only starts the app.js, and does not run my main.js application.

Any help would be appreciated as I am just trying to figure this out from googles help!

4 Answers 4

1

Good answer by Markus,

nevertheless, I need a add a little comment and due to lack of reputation, I need to create an answer.

It is better to start both processes in background (caused by the &) and wait for both processes to finish. Otherwise, it seems that both processes are finished while you only wait for the second command. That might cause trouble in processing output of both commands.

#!/usr/bin/bash
# declare STRING variable
STRING="Starting node processes"
#print variable on a screen
echo $STRING

node misrepo/app.js&
node misrepo/main.js&

# wait until both background processes finish
wait

Best, Frank

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

2 Comments

But the API never finishes?
Both will run in background... Therefore, as long as nothing unexpected happens and both processes die, you will wait forever. In case you want to kill one of the processes, you can use kill command in combination with the PID returned in squared brakets and skip the wait statement.
1

Service management is tricky and there are tools built specifically for it. It's best if you don't reinvent the wheel in bash.

Use forever or pm2 to manage a service.

npm install forever -g
forever start misrepo/app.js

Put them in your script

#!/bin/sh
forever start misrepo/app.js
forever start misrepo/main.js

You can also include these commands (or your script) in the scripts section of your package.json

{
  "scripts": {
    "start": "forever start misrepo/app.js && forever start misrepo/main.js"
  }
}

Then npm start will start your app. stop and restart are also standard.

If your doing this on a server, then use the native service manager to manage each service. This is likely to be systemd if you're on linux.

Comments

0

To start two programs in parallel you can do the following:

#!/usr/bin/bash
# declare STRING variable
STRING="Starting node processes"
#print variable on a screen
echo $STRING

node misrepo/app.js & node misrepo/main.js

This will start node with misrepo/app.js and immediately another instance of node with misrepo/main.js. In your example the script blocks until node misrepo/app.js finishes execution and only then starts node misrepo/main.js

Comments

0

this is maybe too late but hope this will help somebody who is searching for the answer.

#!/bin/bash

//enter in to first folder

cd /var/www/html/demo/

//check the files(if no need remove ls

ls //this will start first node process and after & enters to the next directory

node app.js & cd /var/www/html/demo1

//to ensure the cd command from before line

ls

//starts second node server

node server.js ;

//executes the bin file

exec bash

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.