0

I created a discord bot and am now attempting to run it off an Ubuntu Machine.
I installed the folders of the bot and NodeJs, here is what I used to install NodeJS:

sudo apt-get install -y nodejs

Then I used cd to select the directory, and started my bot using node index.js

The bot started, however when I went to close the putty and keep it running on the VPS the bot shutdown. Here is what the directory looks like.

Root directory tree

Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
M. McFall
  • 45
  • 1
  • 7

4 Answers4

1

I think the problem is that when you start the app in the putty window, that process is linked to the window and gets terminated when that is closed.

To avoid that you can use a host service like screen, tmux, nohup, bg and so on...
If you want to know which is the best, try looking at this question from the askUbuntu Stack Exchange.

The key concept is that you open a new window using the tmux command (or screen, ...), then run your bot like you always do. When you want to leave but keep the process runing, you can detach the session with a key combination, that changes from service to service.
If you want to access that window again, you can run a command that will "restore" your session, like

tmux list-sessions
tmux attach-session -t 0
Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
0

The NodeJS instance is terminated when putty is closed. You need something to keep the instance alive. Try:

PM2: http://pm2.keymetrics.io/

or,

Forever: https://github.com/foreverjs/forever#readme

Recommended though is to run the node instance as a service that can reboot on startup. Try looking at this: https://stackoverflow.com/a/29042953/7739392

joshua miller
  • 1,686
  • 1
  • 13
  • 22
0

The shell runs in the foreground. This means any scripts you start there will end once you end your session. A simple solution would be to run your script in the background by adding the & after the call:

node index.js &

A better solution would be to create a service you can ask the service daemon to run for you. However, adding the & should get you what you want for now.

eDog
  • 173
  • 1
  • 5
0

I recommend using one of these two node modules - ForeverJS or PM2. I'll show you how to quickly get started with ForeverJS but PM2 would be very similar.

You can easily install ForeverJS by typing the following in your terminal:

 $ npm install forever -g

You may need to use SUDO depending on your user's privileges to get this working properly. It is NOT recommended to use it in production due to the security risks.

Once installed CD to your projects file directory and like you typed 'node index.js' you will do something similar with ForeverJS.

$ forever start index.js

Now when you exit the terminal your NodeJS application will remain as a running process.

Blake Basas
  • 89
  • 1
  • 5