A lot of newbs will kill all their node.js processes on their local machines with
pkill -f node
Or
killall node
Etc.
I have a library that uses some daemon processes/workers running on the developer's machine and I will need to restart them if the development "accidentally" kills (all) node.js processes.
The problem is that using NPM libs like forever or supervisor will not solve this problem because they are node.js processes as well TMK.
Can anyone recommend a daemon watcher / relauncher system that will work on MacOS or *nix?
Perhaps supervisord can do what I want to do on both MacOS and *nix? Or perhaps there is another solution to this problem?
I wrote node-windows, node-mac, and node-linux for this purpose. They are essentially wrappers around node processes, but all three libraries share a common API for managing things like restarts/stop/start/etc.
Related
I have a web app project, and it uses Mongo, so one of my NPM scripts is "start-mongo-dev": "mongod", for spinning up the Mongo daemon during development.
I used to use OSX, but just got a new computer and run Linux Ubuntu. Now the command to begin the daemon is sudo service mongod start, so it would seem I should change the NPM script to that.
But that is a red flag. What if I go back to the old computer at all? What if I collaborate on this project with someone who uses OSX?
In short, how is one expected to handle developing with multiple OS's?
Ian's solution may be better but one alternative would be to create a bash script inside your project that detects the OS and runs the appropriate command. You can find more info on how to do that here How to detect the OS from a Bash script?
In some packages, I see divisions like this indicated with a :. For example:
"scripts": {
"start-mongo-dev:osx": "mongod",
"start-mongo-dev:ubuntu": "sudo service mongod start"
}
I am not aware of a canonical solution, but that doesn't mean there isn't one. I happen to like this one, though. #opinion
I've recently been working on a on-demand build server. The build server is a NodeJS/Express REST API, which essentially wraps the Angular CLI and associated build tooling for completing a custom on-demand Angular application.
Everything is working as expected end to end, but I'd like to be able to get more granular with the status reporting, as Angular builds can be quite time consuming when factoring in 2 very large parts of the process
The two longest running parts of the process:
the npm install (generally automatically kicked off via the ng-new schematic from the default #angular/schematics collection)
the actual ng build command.
2 is easy to address, as I am spawning that process (ng build --prod) via child_process.spawn() directly.
1 has proven a bit more complicated, as the long running npm install process is actually kicked off internally to the default Angular ng-new schematic/command. So, if my thinking is correct, this is essentially a explicitly spawned child process (my spawned ng new) which is internally spawning npm install.
One work around that I've come up with is to pass in the --skip-install arg to ng new, which will prevent the internal npm install process from being kicked off by the Schematic. By doing this, I can then manually kick off npm install via child_process.spawn() and directly observe the stdout and stderr streams.
I'm curious if anyone knows of a way to spy on the stderr and stdout streams from the 'npm install' that's kicked off inside of my explicitly spawned ng new command?
Thanks!
If you're running on Linux you can use strace to spy on the ouput of another process.
strace -p7835 -e trace= -e write=3
See this answer for more details.
You can invoke strace from node of course, using spawn. To find the pid of the npm process (which is actually a node process btw), you would need to get the process tree using the ps command. There is already a node module that does this: ps-tree, which also appears to be cross platform.
For alternatives of strace on Windows, check this discussion. I would go for Process Monitor from Sysinternals.
I'm using grunt to run the MEAN project on Ubuntu, but when I close the putty (I use putty to connect Ubuntu server from my PC), it would close the program too.
My question is how can I keep MEAN running?
Update: nohub grunt & stops after I close putty
There are various node based process managers which can serve your task. My favorite is pm2 (http://pm2.keymetrics.io/)
Package managers allow your program to keep running even in case of hiccups. They can watch your project directories for any changes that you might push to them and restart servers based on those changes.
Other favorite is forever (https://www.npmjs.com/package/forever).
you need to run the command in background and I would also recommend to use nohup so:
nohup grunt &
should do the trick.
https://en.wikipedia.org/wiki/Nohup
NODE_ENV=staging nohup node appStag.js &
You can use the above command to run node server
and you can get the above environment using process.env.NODE_ENV
I found a npm package called forever is a good solution, I use forever to run the program right now; and it works perfect with putty.
I'm new to node and have many things unclear.
Like, for php, I just need a index.php file on server's root dir and it can work by itself.
However, for a node.js file, we need to "node" command it in terminal right?
So what if we close that terminal? How can I keep it running to accept my requests?
You are correct in saying that the 'node' command will start a node process with whatever script you supply to it.
As far as keeping it running, there are several ways to do it. There are plenty of CLI libraries that will help you. For example, this one is called Forever
If you are using linux, you can simply run the node process as a background task:
node server.js &
To run node without terminal, you might want to check out one of these modules depending on your platform:
node-mac
node-windows
node-linux
I wrote a simple JAVA application which runs as a service. When my application is up and running, I maintain the PID in a file which will be used while stopping the application.
Issue:
When I restart the OS the stop script is not called (not sure how to make this happen) and the old PID is left as it is in the PID file. Now, after reboot (which start my app) when I stop the app using stop script now my stop script will try to clean up all the PID listed in the file. Most of the time, I will get "No such process". But there are chance the same PID might have been used for some other process
Question:
How I can make sure my stop script will be invoked when I shutdown ore reboot the OS? I am looking a solution for RHEL environment.
I think your are looking for a init script. (startup/shutdown services at different run levels)
This is a good reference
http://blog.rimuhosting.com/2009/09/30/one-java-init-script/
this has a good refernce to Linux init.d scripts
http://coreymaynard.com/blog/creating-a-custom-initd-script-on-fedora/
Hope it helps
If you are looking for scripts that run after reboot, I guess you can write the script in /etc/rc.local and then you can start your service.
This script will run after all your init scripts have run while your machine starts. Using this you can delete the old PID file.