I am running several instance of Node and all of them run on different ports.
I need a way to be able to kill a particular instance of Node based on the port it is running (kill node instance binded to port n).
From my terminal I can easily do a "fuser -k xxxx/tcp" xxxx being the port.
The thing is, that is because I have the authorisation to do so. My question is: I want to be able to have a PHP file that closes the node instance running on port n and it can be opened by anyone. How to do this without opening huge security holes?
Thank you!
The best way to do this would be to have php write the port number to a file, then have a cron script run as root (or whatever user it needs to be) and read the port number(s) from that file. The cron script should have some sort of safety measures in there as well, such as not stopping whatever is running on port 80 or not a node process at all.
edit: for a bit more security, you could require the port number to be prefixed by a "password" that would require you to input. Then the cron script would verify it's prefixed by that password (or hash it and compare the hash would be even better)
You could consider adding one more endpoint to your server, such as /shutdown
, that would call process.exit()
;
Result is immediate, easier to implement than using a bunch of shell commands, tracking pids, etc...
Also easy to protect via password, signed request, etc.