Search code examples
javascriptraspberry-pidiscord.jspm2

pm2 autostart discord.js bot won't connect again on reboot (raspberry pi)


I am hosting a discord.js bot on my raspberry pi 4. It connects flawlessly when I do it manually with pm2 start index.js Now the issue is when I tried to add it to autostart

pm2 autostart
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u pi --hp /home/pi
pm2 save

Now when I restart I the process has started up, I get this when I type pm2 list enter image description here

but the bot will be offline on discord.

In the error logs I have this error repeating multiple times:

Error: getaddrinfo EAI_AGAIN discordapp.com discordapp.com:443
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)

However if I restart with pm2 restart 0 the discord bot will be online again.


Solution

  • It seems like the internet connection is not yet established when the bot starts. When you open raspi-config, go to Boot Options and check Wait for Network at Boot, the value should be "yes".

    That way, the processes will be started when an internet connection is established.

    In newer versions of raspi-config this option can be found in System Options, then Network at Boot.