For your Discord bot JAR file on Linux, especially when using a systemd service with a user without a home directory, the recommended approach is:
1. Place the JAR File in /opt
2. Create a Dedicated System User
3. Set Permissions
4. Systemd Service File (/etc/systemd/system/discord-bot.service)
[Unit]
Description=Discord Bot Service
After=network.target
[Service]
User=discordbot
Group=discordbot
WorkingDirectory=/opt/discord-bot
ExecStart=/usr/bin/java -jar /opt/discord-bot/your-bot.jar
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
5. Handling Configuration/Data
-
Config files: Place in /etc/discord-bot/ (create the directory).
sudo mkdir /etc/discord-bot
sudo cp config.json /etc/discord-bot/
sudo chown -R discordbot:discordbot /etc/discord-bot
Reference it in your bot code via absolute path (/etc/discord-bot/config.json).
-
Runtime data (if needed): Use /var/lib/discord-bot/ (create and set ownership similarly).
6. Logs
- Systemd/Journals: By default, logs go to
journalctl (no extra setup needed):
journalctl -u discord-bot -f
- File logs: Redirect output to
/var/log/discord-bot.log:
sudo touch /var/log/discord-bot.log
sudo chown discordbot:discordbot /var/log/discord-bot.log
Then add to the systemd service:
StandardOutput=file:/var/log/discord-bot.log
StandardError=file:/var/log/discord-bot.log
7. Deploy & Start
sudo systemctl daemon-reload
sudo systemctl start discord-bot
sudo systemctl enable discord-bot # Start on boot
Key Points:
- Security: The
discordbot user has no shell/home directory, limiting attack surface.
- FHS Compliance: Files are in standard locations (
/opt, /etc, /var/log).
- Simplicity: Systemd handles restarts, logging, and boot startup.
- Permissions: The service user owns only necessary directories.
This structure keeps your bot organized, maintainable, and secure. Adjust paths in your bot code to match the Linux locations (e.g., /etc/discord-bot/config.json instead of ./config.json).