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.

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 m…


This content originally appeared on DEV Community and was authored by Mikael Lavi

TL;DR I wanted CLI for my Node servers and used unix socket to build it. I also made a non-blocking version.

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.

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"
}

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. Come to think of it you can apply this easily with other languages too. They are after all a custom rolled logic for messaging. Hope you enjoyed!


This content originally appeared on DEV Community and was authored by Mikael Lavi


Print Share Comment Cite Upload Translate Updates
APA

Mikael Lavi | Sciencx (2025-10-14T07:35:05+00:00) Node CLI for any server. Retrieved from https://www.scien.cx/2025/10/14/node-cli-for-any-server/

MLA
" » Node CLI for any server." Mikael Lavi | Sciencx - Tuesday October 14, 2025, https://www.scien.cx/2025/10/14/node-cli-for-any-server/
HARVARD
Mikael Lavi | Sciencx Tuesday October 14, 2025 » Node CLI for any server., viewed ,<https://www.scien.cx/2025/10/14/node-cli-for-any-server/>
VANCOUVER
Mikael Lavi | Sciencx - » Node CLI for any server. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/14/node-cli-for-any-server/
CHICAGO
" » Node CLI for any server." Mikael Lavi | Sciencx - Accessed . https://www.scien.cx/2025/10/14/node-cli-for-any-server/
IEEE
" » Node CLI for any server." Mikael Lavi | Sciencx [Online]. Available: https://www.scien.cx/2025/10/14/node-cli-for-any-server/. [Accessed: ]
rf:citation
» Node CLI for any server | Mikael Lavi | Sciencx | https://www.scien.cx/2025/10/14/node-cli-for-any-server/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.