Skip to content

Running as a service

Running as a service#

Running JMusicBot as a service allows it to run in the background without needing to be manually started. This is useful for running the bot on a server, or if you want to be able to close your terminal without stopping the bot.

Linux using systemd#

Warning

This method assumes that you've created a user for the bot to run as. If you haven't, see this guide for instructions.

Note

Copy the jar file to the home directory of the user that the bot is running as, or change the WorkingDirectory and ExecStart lines in the service file to point to the correct location.

  1. Open a terminal and run the following command to create a new service file:
sudo nano /etc/systemd/system/JMusicBot.service
  1. Copy the following text into the file and save it:
[Unit]
Description=JMusicBot
Requires=network.target
After=network.target

[Service]
WorkingDirectory=/home/<username>
User=<username>
Group=<username>
Type=simple
ExecStart=/usr/bin/env java -Dnogui=true -jar JMusicBot.jar
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Note

Replace <username> with the name of the user that the bot is running as.

  1. Run the following command to start the bot:
sudo systemctl start JMusicBot
  1. Run the following command to stop the bot:
sudo systemctl stop JMusicBot
  1. Run the following command to restart the bot:
sudo systemctl restart JMusicBot
  1. Run the following command to enable the bot to start on boot:
sudo systemctl enable JMusicBot

Linux using screen#

Warning

This method is not recommended for production use, see systemd instead.

  1. Install the screen utility, if it isn't already installed.
  2. Run the following command to start the bot:
screen -dmS JMusicBot java -jar JMusicBot.jar
  1. Run the following command to stop the bot:
screen -S JMusicBot -X quit
  1. Run the following command to restart the bot:
screen -S JMusicBot -X quit
screen -dmS JMusicBot java -jar JMusicBot.jar

Windows#

  1. Download the NSSM executable and place it in the same directory as the JMusicBot jar file.
  2. Open a command prompt in the same directory as the JMusicBot jar file and run the following command:
nssm install JMusicBot java -jar JMusicBot.jar
  1. Run the following command to start the service:
nssm start JMusicBot
  1. Run the following command to stop the service:
nssm stop JMusicBot
  1. Run the following command to remove the service:
nssm remove JMusicBot
Back to top