Search code examples
node.jslinuxdiscord.jsraspbianraspberry-pi4

How can I make my discord.js start at boot using node pi-4?


I am new to linux in general so I may not be aware of certain things. So, I have tried multitudes of solutions but I haven't succeeded in running the discord.js bot. I have used rc.local with other scripts like writing date on a file on startup date > <file-path> and it works properly, but somehow running bot on startup fails and can't quite read any errors either.

rc.local file:

#!/bin/sh -e

_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

/bin/sh /home/pi/superscript.sh

exit 0

superscript.sh file:

#!/bin/sh

date > /home/pi/boot.log

( cd /home/pi/Desktop/discord-bot; /usr/bin/node /home/pi/Desktop/discord-bot/index.js ) &

The bot works completely fine when I manually execute it node ~/Desktop/discord-bot/index.js & and it even works if I manually execute superscript.sh, so I can't really find the problem. Can anyone help me out please I would really appreciate it.

Thank You.


Solution

  • easiest way is to use crontab to execute commands on startup

    # Edit cron
    crontab -e
    
    # type following to start a script-file.sh on startup
    @reboot script-file.sh
    

    reference: https://www.simplified.guide/linux/automatically-run-program-on-startup