John
John

Reputation: 565

Can I run multiple node processes from 1 copy of my source code

I want to know if it's possible to run multiple node processes from the same directory, either with the same file or using a different file. The process I am running will basically execute batch jobs, and will not be running a server, but I would like to know for the server case as well.

Let's say I have the following structure

I have a two-part question.

Will I be able to open up 2 terminals in the src directory and execute PORT=3000 node server.js from the first terminal, and then, while being in the same directory, in the second terminal run PORT=3001 node server.js

Secondly, I want to set up a cron job, to run batch jobs, so I would like to call node src/file1.js and node src/file2.js from my cron job. Since these files will reside in the same directory, will I need to have separate copies of the source code in order to run 2 separate jobs, or can I do it from the same directory and have only one copy of the source?

In general, for each separate node process we run from a directory either on the same file or a different file, do I need to run it out of a different copy of my source code, or can I use one copy of my code and run multiple node processes from different terminals or cron jobs?

Upvotes: 2

Views: 2897

Answers (2)

jfriend00
jfriend00

Reputation: 707376

Will i be able to upon up 2 terminals in the src directory and basically execute PORT=3000 node server.js from the first terminal, and then while being in the same directory in the second terminal run PORT=3001 node server.js

Yes, that will work fine. Source code for a node.js program is loaded from disk, parsed and turned into some sort of internal structure in memory. The source files themselves are then not used again after being loaded while that program is running. You can load that source code as many times as you want for different instances of the app. You will have to make sure that each instance does not contend for the same resources (ports, files, etc...), but it looks like you're already aware of that and that would be no different if it was different programs running on the same computer.

Secondly, I want to set up a chron job, to run batch jobs, so I would like to call node src/file1.js and node src/file2.js from my chron job. Since these files will reside in the same directory, will I need to have separate copies of the source code in order to run 2 separate jobs, or can I do it from the same directory and have only one copy of the source ?

One copy of the source will be just fine.

In general, for each separate node process we run from a directory either on the same file or a different file, do i need to run it out of a different copy of my source code, or can I use one copy of my code and run multiple node processes from different terminals or chron jobs ?

One copy of the source is fine. Just to give you an idea, the clustering module for node.js does exactly this. It runs multiple processes using the same source code.

Upvotes: 6

Wahib Mkadmi
Wahib Mkadmi

Reputation: 646

Not a node.js guru but your scripts should run separately even if they are in the same directory, as long as they are not sharing resources. Concurrent processes for example writing to a file (reading isn't a problem) may cause failures. But system calls should work just fine.

Upvotes: 0

Related Questions