Search This Blog

Monday, September 3, 2018

Making a Javascript file into a background service in Linux



This is really useful for making a javascript file run as a background service.

============

Original Article here:
  https://hackernoon.com/making-node-js-service-always-alive-on-ubuntu-server-e20c9c0808e4

Article Written by

Tigran Bayburtsyan - Mar 15, 2017
It’s time to make SystemD Service
Making file /etc/systemd/system/hello_world.service , this would be the main file as a configuration to SystemD service
[Unit]
Description=Node.js Hello World Http Server
[Service]
PIDFile=/tmp/hello_world-99.pid
User=
Group=
Restart=always
KillSignal=SIGQUIT
WorkingDirectory=/home//hello_world/
ExecStart=/home//hello_world/server.js
[Install]
WantedBy=multi-user.target
After saving this file, we can enable this service, to start using it over SystemD
sudo systemctl enable hello_world.service
That’s it! Now you have Node.js service as a background service, it will be restarted when something goes wrong and you can view all output logs using very simple command
sudo journalctl -u hello_world.service
# For real time logs just add -f argument
sudo journalctl -fu hello_world.service

Conclusion

After all this setup kind of things now you got very easy way to manage your service
sudo systemctl start hello_world.service
sudo systemctl stop hello_world.service
sudo systemctl restart hello_world.service

No comments: