DEV Community

Cover image for Node CLI for any server
Mikael Lavi
Mikael Lavi

Posted on

Node CLI for any server

TL;DR I wanted CLI for my Node servers and used unix socket to build it. I also made a non-blocking version. Come to think of it you can apply this easily with other languages too like Go and Python.

I was playing around with the idea about having CLI commands for the server running on Linux. We've had this for some time with pm2 (process manager 2) essentially and moving away from it has left us missing commands like create organization or checking the server status from command line. The possibilities for such commands is endless.

It started out as an investigation of other libraries that might provide the scaffold on which to build the capability, but nothing really came out of it. Prompting Gemini yielded same old results that "use REST API and just curl it". Which makes sense of course. It is a bit of an mindset that I do not want to expose certain parts of the program even by design to the internet, so I wanted an alternative.

I asked the AI to focus on reviewing how the pm2 daemon works with RPC commands. It got around to it and still gave the same recommendation. Still not satisfying. It was the question about alternatives to pm2 daemon that brought up the Node cluster along with systemd library.

It is the interprocess communication I find valuable in the example rather than the worker to ensure non-blocking to the main process. The IPC Unix socket, or a file socket, makes it so that it can be called only from the local system. I got distracted that the cluster was the key but its not.

Now the server could certainly be made so that it checks for the existence of the listener already or it starts it. When it is running though, you can issue commands like status and stop to it and it responds back. It should not matter that the server is running inside a process manager even, since it would still establish the exact same control. This sounds like exactly what I was after.

It is important to get the right prompt for the AI to generate the right samples, so here's the one I used.

"Let's assume I would use the cluster module. I'd like you to present an example version where the main server startup just creates the master node, and any subsequent instances would check its presence and offer CLI help on commands like "status" to call on the master."

The final solution took a bit more doing, but it works! Here's it running:

$ node host.js
ℹ️ No active server found. Starting a new one...
🚀 Server process with PID: 515 is starting.
   ✅ Application logic is running in this process.
Enter fullscreen mode Exit fullscreen mode

And in the next terminal I could call up the same call and after that call with a command.

$ node host.js
ℹ️ Server is already running.

Usage: node app.js <command> [arguments]

Available commands:
  status
  create:org <name>
  stop

$ node host.js status
{
  "workerPid": 515,
  "uptime": "14s",
  "memoryUsage": "49MB"
}
Enter fullscreen mode Exit fullscreen mode

Summary

I had fun creating this code! Please find the codes I made on my Github gists below. Feel free to use and mimic how you like them. They are after all a custom rolled logic for messaging. Hope you enjoyed!

Top comments (0)