npm i -g shut-me-down
Important depending on your OS, you need to allow commands to be used without sudo:
# Run 'sudo visudo' and addyour-username ALL=NOPASSWD: /sbin/shutdown # OS X and Linux
Configuring service on Linux or similar
For this purpose we can use
systemctl in order to set up a persistent service running on our machine.
You can do this with the following command:
sudo nano /etc/systemd/system/shut-me-down.service
## /etc/systemd/system/shut-me-down.service[Unit]Description=Shut down APIAfter=network.targetStartLimitIntervalSec=0[Service]Type=simpleRestart=alwaysRestartSec=1User=username #1ExecStart=/usr/local/lib/nodejs/bin/node /usr/local/lib/nodejs/bin/shut-me-down "127.0.0.1" #2 #3[Install]WantedBy=multi-user.target# 1 Make sure that this field its changed for the username of your machine.# 2 Be sure that your machine match this configuration, if you installed nodejs with node version manager probably won't.# 3 You can limit where the service listen to the request, for example to limit to request coming from the local machine with localhost you can use 127.0.0.1, if this argument is not present 0.0.0.0 it's used by default, this means that the service will listen to any request coming from any internet interface.
Once you have created the
systemd service you can run it with the following command
## If you used a different name for the service file you need to use it heresudo systemctl start shut-me-down.service## Check the execution statussudo systemctl status shut-me-down.service
The status should return an output like this:
If is not the case, and you get an error you will need to resolve the issues with the logs shown at the bottom of the status log or you can see the complete logs with the following command.
sudo tail -n 40 -f /var/log/syslog
If the service it's running succesfully you should be able to have the port 5709 open to your API, you can check it with the following command:
sudo netstat -onatup | grep 5709#tcp 0 0 0.0.0.0:5709 0.0.0.0:* LISTEN 31025/node off (0.00/0/0)
The application listens on the selected ip, and the port
You can test that the service woks by sending a
POST to the url
http://your.choosen.ip:5709/halt. If you want to test it from your terminal this command will help you:
## Remember that you need to change the host for your the IP that requires your specific case, if the request comes from the same machine you cans use localhost.## But if the request comes from other device you will ned to use the ip of the device running the service.curl -d '' http://localhost:5709/halt ## This will power off your device if it's correctly configured.