I have a spare MacBook Pro that I’m using to run a 24/7 Plex media server for streaming movies and shows to my TV. I thought hey, I can run a Discord bot on this machine as well! So I simply ran npm start
in a terminal window and expected my Node.js Discord bot to be up and running as long as my computer was on.
One problem ocurred: each time macOS went to sleep, my node script would stop running as well. When I moved the mouse to wake up it, the bot would go back online, and then go back to sleep soon after. Even enabling the Amphetamine app wasn’t keeping my bot online.
So I did some googling, and found a new command that I had not known about: caffeinate
We simply need to give our npm command some caffeine so it doesn’t go to sleep:
caffeinate -s npm start
And voilĂ ! My Discord bot has been online ever since I’ve ran the command.
If your node script is already running and you don’t want to restart it with the caffeinate
command, you can find the process ID of it by using: ps | grep node
and keep it awake like this:
caffeinate -w <PID>