Run JS file when starting Ubuntu

0

first of all I notice that I'm starting with NodeJS ...

I was trying the basics and what I found in some tutorials, until I wanted to try the DiscordJS library to create a bot, so later on my computer I do

node discord_test.js

And without problems my bot already works and performs the basics that I need but, once I upload all the corresponding files, as I have to do so when starting the vps (by doing a reboot) or by making it run for the first time do not have to manually do "node discord_bot.js"? Because if I do this I will always be aware of everything and besides this I have several resources to go driving through the console in the VPS ...

As I did not know how to search this in Google (neither in Spanish nor English nor with specific words) I ended up asking around here ...

    
asked by KuroNeko 24.10.2018 в 04:21
source

1 answer

2

You can try setting up your nodejs application as an Ubuntu service and configure it to start with the system (similar to what you do when you send the application to production).

First you must give executable permissions (replace path with the path of your file):

chmod +x ~/path/discord_test.js

Then you must create the file where you declare the service:

vim /etc/systemd/system/discord_test.service

Copy and paste the following settings:

[Unit]
Description=Servicio Node.js 

[Service]
PIDFile=/tmp/discord_test.pid
User=<Nombre de usuario>
Group=<Gurpo>
Restart=always
KillSignal=SIGQUIT
WorkingDirectory=/home/<usuario>/discord_test/
ExecStart=/home/<usuario>/discord_test/discord_test.js

[Install]
WantedBy=multi-user.target

Replace the values from the previous configuration to match the routes of your computer ( WorkingDirectory, ExecStart ), as well as user and group values.

Then you must enable the service that you have created:

sudo systemctl enable discord_test.service

Now you have your application in a service that will restart every time an error occurs

You can also monitor your service through a log file

sudo journalctl -u discord_test.service

Now you have the following commands to manipulate your service ( start, stop and restart ):

sudo systemctl start discord_test.service
sudo systemctl stop discord_test.service
sudo systemctl restart discord_test.service

I hope it works for you, Good luck!

    
answered by 28.10.2018 / 03:52
source