Discord bot database issues when running with PM2

I’m having trouble with my Discord bot’s database. It works fine when I start it using node bot.js on my local machine and Linux VM. But when I try to run it in the background with PM2 on the VM, it can’t read the SQLite database. The bot doesn’t respond to the m!shake command like it should. I’ve checked the code and it seems okay. Has anyone else run into this problem? I’m wondering if it’s a path issue or something to do with how PM2 handles file access. Any ideas on how to fix this or what I should check next? I really want to keep the bot running in the background, but I need the database to work properly. Thanks for any help!

hey man, ive had this issue before. check ur PM2 logs for any errors. also, make sure ur using the full path to the database file in ur code. sometimes PM2 gets confused with relative paths. if that doesnt work, try giving PM2 explicit permissions to the database folder. good luck!

I’ve encountered similar issues with PM2 and SQLite before. It’s likely a path problem. When PM2 runs your bot, it might be using a different working directory than when you run it directly. Try using absolute paths for your database file instead of relative ones. For example, replace ‘./database.sqlite’ with ‘/home/yourusername/botfolder/database.sqlite’. Also, make sure PM2 has the necessary permissions to access the directory where your database is stored. If that doesn’t work, you could try setting the ‘cwd’ option in your PM2 ecosystem file to explicitly specify the working directory. Hope this helps you get your bot running smoothly!

I’ve dealt with this exact problem before, and it can be super frustrating. One thing that worked for me was double-checking the user that PM2 is running under. Sometimes it defaults to a different user than you expect, which can cause permission issues with the database file.

Try running ‘pm2 list’ to see which user is running your bot process. If it’s not your usual user, you might need to adjust file permissions or switch the PM2 user. Also, make sure your database file isn’t in a location that’s restricted, like /root/.

Another trick is to use the ‘pm2 ecosystem’ command to generate a config file. In that file, you can specify the exact working directory and environment variables, which can solve path-related headaches. It gives you more control over how PM2 runs your bot.

If all else fails, you could try using a different process manager like Forever or Systemd. Sometimes a fresh approach does the trick. Good luck sorting it out!