Monitor starting/running/ending apps within node.js - node.js

Is there a way to monitor starting/running/ending apps within node.js?
Like "You just started LibreOffice Writer!".
It would be nice if any of you cracks could help me with this.
Edit:
I am searching for something that runs in the background and is triggered whenever I run a random application/script/whatever has a system-wide pid.

forever is a daemon manager for node.js that can do this.
http://thechangelog.com/post/6637623247/forever-node-js-daemon-manager
https://github.com/indexzero/forever
Or if you just want to start a process once and be able to manage it, you can use the builtin ChildProcess.
http://nodejs.org/docs/v0.4.9/api/child_processes.html

Related

What is the correct way to start and stop simple Node.js scripts

I'm trying to create a simple Twitter bot to learn some Node.js skills.
It works fine on my local computer. I start the script with node bot.js and then close it with Ctrl + C.
I've uploaded the files to a server (Krystal hosting). I've ssh'd into the server and then used $ source /home/[username]/nodevenv/twitterbot/10/bin/activate. Which I think puts me into a Node environment (I'm not really clear what is happening here).
From here I can run node bot.js. My Twitter bot runs fine and I can leave the terminal. What I've realised now is that I don't know how to stop this script.
Can someone explain how I should be doing this? Is there a command I can enter to stop the original bot.js process? Since looking into this it looks like perhaps I should have used something like pm2 process manager. Is this correct?
Any pointers would be much appreciated.
Thanks,
B
You can kill it externally by nuking the process from an OS command line or in an OS GUI (exact procedure varies by OS). Ctrl-C from the shell is one version of this, but it can be done without the command shell that it was started in too by nuking the process directly.
Or, you can add a control port (a simple little http server running on a port only accessible locally) that accepts commands that let you do all sorts of things with the server such as extract statistics, shut it down, change the configuration, tell it to clear caches so content updates take effect immediately, etc... Shutting down the server this way allows for a more orderly shut-down from code within the server. You can even stop accepting incoming connections, but wait for existing http connections to complete before shutting down, close databases normally, etc...
Or, you can use a monitoring program such as PM2 or forever that in addition to restarting the server automatically if it should ever crash, they also offer commands for shutting it down too (which will just send it certain signals kind of like Ctrl-C does).

Identify if Process is Not Responding in Bash or Node.js

Using an Electron App to control instances of other apps, but sometimes an instance will freeze. It shows up as Not Responding in Activity Monitor, but how can I tell that it's not responding from either Bash or Node.js so I can kill and restart the process? Thanks!
The following answer to a different question might help you:
you could check the files
/proc/[pid]/task/[thread ids]/status
- How to check if a process is in hang state (Linux)

Can I monitor daemon/service with supervisord?

I have a system-V init service/daemon running my application. I wanted to make sure that my application always runs even with conditions where process/service could crash, machine restart. I know of supervisord which is able to monitor process but I am not sure it can monitor service/daemon ?
Looks like the manual advices against it.
There is an answer to a similar question which provides a workaround.
Anyway, I would try to find a way to have that service stay in the foreground.

How to run another node.js script and then kill it

I want my testing script to be able to run my server and then kill it at the end of testing. In fact I will probably want to do this mulitple times in the testing script. I tired exec with shelljs. But that seems to me from running any commands after I start my server for testing it also seems like it would be hard to find the sever to kill it after starting it (especially on different OSes). How might I do such a thing?
On different OS you will need to find processId to kill it. This is tedious and not recommended process.
Use a wrapper around like pm2 or nodemon for servers.
In testing case you can make use of process.exit(0) and expose it in your make code.

NodeJS: how to run three servers acting as one single application?

My application is built with three distincts servers: each one of them serves a different purpose and they must stay separated (at least, for using more than one core). As an example (this is not the real thing) you could think about this set up as one server managing user authentication, another one serving as the game engine, another one as a pubsub server. Logically the "application" is only one and clients connect to one or another server depending on their specific need.
Now I'm trying to figure out the best way to run a setup like this in a production environment.
The simplest way could be to have a bash script that would run each server in background one after the other. One problem with this approach would be that in the case I need to restart the "application", I should have saved each server's pid and kill each one.
Another way would be to use a node process that would run each servers as its own child (using child_process.spawn). Node spawning nodes. Is that stupid for some reason? This way I'd have a single process to kill when I need to stop/restart the whole application.
What do you think?
If you're on Linux or another *nix OS, you might try writing an init script that start/stop/restart your application. here's an example.
Use specific tools for process monitoring. Monit for example can monitor your processes by their pid and restart them whenever they die, and you can manually restart each process with the monit-cmd or with their web-gui.
So in your example you would create 3 independent processes and tell monit to monitor each of them.
I ended up creating a wrapper/supervisor script in Node that uses child_process.spawn to execute all three processes.
it pipes each process stdout/stderr to the its stdout/stderr
it intercepts errors of each process, logs them, then exit (as it were its fault)
It then forks and daemonize itself
I can stop the whole thing using the start/stop paradigm.
Now that I have a robust daemon, I can create a unix script to start/stop it on boot/shutdown as usual (as #Levi says)
See also my other (related) Q: NodeJS: will this code run multi-core or not?

Resources