I have multiple processes that run on the server.
Each of the processes is placed in it's own directory.
All of them are controlled by PM2
Each process is supposed to have config and ecosystem files within their folders
Root folder is supposed to be empty and not related to any processes.
One process that was set up incorrectly. I had to make it work (without touching things that do) so for this process :
Pm2 show (id) indicated that the exec cwd is running from root so:
Copy essential folders and files of the root folder
Config folder, logs, other essentials had to be copied
I would like to redirect the process from root to its own folder.
It seems I can do it simply by modifying the exec cwd but I don't know how to do it.
Please help :)
Cheers
Ok well.
It seems it is impossible to modify it like this as this functionality is not implemented.
Therefore:
copy ecosystem and what you need from folder you are in to where you want it to run from
delete the process completely
do cd {path to your desired directory}
pm2 start ecosystem.config.js
//Additional if you want to keep the process old Id
pm2 kill (deletes all processes)
pm2 resurrect (restores them all with original id's)
Related
I have a bunch of processes started by PM2 on a server. pm2 list shows this:
I'm sure that each of them is started with an ecosystem config file. What I want to know is that what pm2 ecosystem config file is used to start for example process with the id 62 and the ecosystem config file path.
So is there any pm2 command for that?
Well, you can differ processes by app name (so you should set unique name in each ecosystem config file) or you can use a NODE_APP_INSTANCE variable to differ processes in cluster.
[EDIT]
There is no way how to find specific ecosystem file but you can use pm2 describe [processID/name] to find directory where was pm2 start executed (exec cwd parameter). If there is more ecosystem files than 1 you can differ processes only by name.
I created an app using Javascript (with D3.js), JQuery, CSS, but no Node.js. It's your typical 'index.html' browser-run interface. I've been going through Docker tutorials and trying to figure out how to set my app up to a server, but I've had no luck accomplishing it and have only been able to find tutorials using apps with Node. I cannot, for the life of me, figure out what I'm doing wrong, but I'm wondering if the problem (or one of them) lies in my Dockerfile. Also, do I need to have used Node.js for all this to work? My app consists of the following:
A directory called Arena-Task. Within this directory, I have my index.html, my main javascript file called arena.js, and my CSS files. My other necessary files (like images, etc.) are located within two other folders in the same directory called data and scripts.
So now, how would I write my Dockerfile so that I can build it using Docker and publish it to a server? I've attempted following Docker's example Dockerfile:
FROM node:current-slim
WORKDIR /usr/src/app
COPY package.json .
RUN npm install
EXPOSE 8080
CMD [ "npm", "start" ]
COPY . .
But to be honest I'm not quite sure how to make the changes to accommodate my program. I can't figure out if a package.json is required because if it is, then don't I need to be using Node? I don't use any node modules or project dependencies like that in my app. Do I need to be? Is my problem more than just an incorrect Dockerfile?
Sorry that this question is all over the place, but I'm really new to the realm of the server-side. I appreciate any help! Let me know if I can provide any clarification.
lets clarify few things:
node and npm is when you need them, like you are using some npm packages.
package.json - is in use by npm - it stores installed package list in it.
For you case i don't see need of node. so you can create simple image and then you going to need simple web server - something which can serve you html/css/js files on web requests over http. the simplest i know would be nginx.
Also in Dockerfile you need to copy all you resources into image you are building.
that is what COPY package.json . was doing. but in you case you have to copy whole app folder into some app folder in docker image. (assuming app is a folder which holds all you files)
so we are going to have steps
Dockerfile should look something like this:
FROM ubuntu
RUN apt-get install -y nginx
COPY app app
COPY startup.sh /startup.sh
COPY ./nginx-default /etc/nginx/sites-available/default
no need in default commands because you going to start something else during docker run.
nginx-default is a configuration for nginx to work as webserver:
it should look something like this:
server {
listen 8080;
server_name localhost;
root /app
}
nginx is very flexible - if you need something from it google it.
docker image should do something all the time, otherwise image going to stop (some blocking process).
the easiest way i know is to create startup.sh file which going to start nginx as first step and then going to do infinity loop:
exit_script() {
trap - SIGINT SIGTERM # clear the trap
sudo service nginx stop
exit 1
}
sudo service nginx start
while sleep 20; do
CURRENT_TIME=$(date +"%T")
echo "app is running: $CURRENT_TIME"
done
exit_script - is a trap which helps to stop docker image in fast way, but not like terminate. but you can omit that for testing purposes.
finally, build image (docker build -t {your-image-name} .) and to start image use something like this:
docker run -p 8080:8080 {your-image-name} bash /startup.sh
that should work :), though most probably you going to face few errors because i was writing it from the head. (like you may need something else for nginx, or sudo is not installed by default in ubuntu lates image).
I'm trying to have pm2 watch everything in events, commands, data, index.js, and config.js.
pm2 --name druggy --watch
events,commands,data,index.js,config.js/index.js start node -- index.js
I used this and whenever I try to change a file in one of the folders, when I save my bot it doesn't restart. However it does restart if I change the index.js. How can I make it restart when I change anything inside those folders and the two files?
Do not give --watch any parameters. You do not pass them in through the command line, anyways. Change your command to the following:
pm2 start index.js --name druggy --watch
If you do want to specify which paths to watch, then follow the documentation located here.
Just earlier, I posted my question:
https://stackoverflow.com/questions/28336443/how-to-not-put-my-js-files-in-user-myuser-for-node-js
I have a file, hello.js, located in /Users/MyUser/Desktop/Node/
I can see that my default directory is /Users/MyUser/
Okay, so I get that I need to change my working directory. What I have been able to find so far is to use >process.chrdir('/Users/MyUser/Desktop/Node/');
Cool, that works, but now when I get out of the REPL shell, the directory resets.
The person who responded to my question said that I needed to run >node init and later npm install <name of dependency> --save
My first question: I have ran >node init and see that I can create this package.json file, what does this do exactly?
Secondly: I was told that I need to add dependancies. Could someone please explain to me what this means in Node terms? Does a dependancy simply mean a folder that I want node to include? Do I want to add this Node folder on my Desktop to be able to run my scripts?
I am currently trying to go through the learnyounode courses, however I do not want to have to save all of these test files in my /User/MyUser directory, so any advice would be greatly appreciated.
Thanks
I have ran >node init and see that I can create
this package.json file, what does this do exactly?
npm init is used to create a package.json file interactively. This will ask you a bunch of questions, and then write a package.json for you.
package.json is just a file that handle the project's dependencies and holds various metadata relevant to the project[ project description, version, license information etc]
I was told that I need to add dependencies. Could someone please
explain to me what this means in Node terms?
Lets say you're building an application that is dependent on a number of NPM modules, you can specify them in your package.json file this way:
"dependencies": {
"express": "2.3.12",
"jade": ">= 0.0.1",
"redis": "0.6.0"
}
Now doing npm install would install a package, and any packages that it depends on.
A package is:
a folder containing a program described by a package.json file
a gzipped tarball containing (1)
a url that resolves to (2)
a # that is published on the registry with (3)
a # that points to (4)
a that has a "latest" tag satisfying (5)
a that resolves to (2)
If you need to install a dependency that haven't been included in package.json, simply do npm install <packageName>. Whether or not you want to include this newly installed package in package.json is completely your take. You can also decide how this newly installed package shall appear in your package.json
npm install <packageName> [--save|--save-dev|--save-optional]:
--save: Package will appear in your dependencies.
--save-dev: Package will appear in your devDependencies.
--save-optional: Package will appear in your optionalDependencies.
Does a dependency simply mean a folder that I want node to include?
Umm, partly yes. You may consider dependencies as folders, typically stored in node_modules directory.
Do I want to add this Node folder on my Desktop to be able to run my
scripts?
No, node manages it all. npm install will automatically create node_modules directory and you can refer to those dependencies with
require() in your .js files
var express = require('express');
Node REPL simply provides a way to interactively run JavaScript and see the results. It can be used for debugging, testing, or just trying things out.
process.cwd() points to the directory from which REPL itself has been initiated. You may change it using process.chdir('/path'), but once you close the REPL session and restart, it would always re-instantiate process.cwd() to the directory from which it has been started.
If you are installing some packages/dependencies in node project1 and think those dependencies can also be useful for node project2,
install them again for project2 (to get independentnode_modules directory)
install them globally [using -g flag]. see this
reference packages in project2 as
var referencedDependency = require('/home/User/project1/node_modules/<dependency>')
Simply doing process.chdir('/home/User/project1/node_modules/') in REPL and referencing as
var referencedDependency = require('<dependency>') in your js file wont work.
>process.chdir('/Users/MyUser/Desktop/Node/'); change the working directory only for that particular REPL session.
Hope it helps!
This has nothing to do with node.js but is rather inherent in the design of Unix (which in turn influences the design of shells on other operating systems).
Processes inherit values from their parent's environment but their environments are distinct.
That terse description of how process environments work has a sometimes unexpected behavior: you cannot change your parent's environment. It was designed this way explicitly for security reasons.
What this means is, when you change the working directory in a process and quits that process your shell's working directory will not be affected. Indeed, your shell's working directory isn't affected even when the process (in this case, node REPL) is running.
This exact question is often asked by people writing shell scripts wanting to write a script that CDs into someplace. But it's also common to find this question asked by people writing other languages such as Perl, Tcl, Ruby etc. (even C).
The answer to this question is always the same regardless of language: it's not possible to CD from another program/script/process.
I'm not sure how Windows handles it so it may be possible to do it there. But it's not possible on Unixen.
I'd like to automatically restart the server after particular files are edited.
Is there anything I can install to help me do that? - or will I need to watch the folder run a script accordingly.
Any pointers appreciated
Use supervisor. Install it with npm install supervisor -g and launch your code with supervisor server.js and you should be good to go. Note that by default it keeps an eye on the files that are in the same directory as the server.js and it's subdirectories, but it should be possible to add additional paths.
You could use Nodemon for that, there is even a video tutorial on it.
https://github.com/mdlawson/piping is good too.
There are already node "wrappers" that handle watching for file
changes and restarting your application (such as node-supervisor), as
well as reloading on crash, but I wasn't fond of having that. Piping
adds "hot reloading" functionality to node, watching all your project
files and reloading when anything changes, without requiring a
"wrapper" binary.
Nodemon is good for it https://github.com/remy/nodemon
Also, if you want nodemon to restart your app only if particular files are changed, it is important to have .nodemonignore file in which you can tell changes to which files should be ignored by nodemon.
Example .nodemonignore file :
/public/* # ignore all public resources
/.* # any hidden (dot) files
*.md # Markdown files
*.css # CSS files
.build/* # Build folder
/log/*