Auto-start your things with systemd
I run different services on my VPSs, and while most programs you install via the package manager come with systemd service to auto-start when the server boots like the IRCd or Mumble Server, if you download a binary manually they might not come with one. I run Minecraft servers, small web servers and different bots. These are run in a tmux session where I just run the binary and let it sit.
The Minecraft server I do not feel comfortable auto starting, I want to control when it is running and not and access the console easily. But various other bots or the web server can be auto-started with a simple systemd service.
I will use the IRC bot Dis4IRC which is an IRC ↔ Discord bridge for this example. It is a .jar
binary that is run by java -jar Dis4IRC.jar
.
/etc/systemd/system/ircbot.service
:
[Unit]
Description=IRC Bot
[Service]
WorkingDirectory=/path/to
ExecStart=java -jar /path/to/Dis4IRC-1.6.4.jar
User={{ USER }}
[Install]
WantedBy=multi-user.target
{{ USER }}
is replaced but the user that should run the service, in this case a regular user, not root. Make sure you use WorkingDirectory
because you might have a config file the bot needs to read.
If the bot is written in Python and you use a virtualenv, you can change ExecStart
to include the virtualenv, like this.
ExecStart=/path/to/pythonbot/envname/bin/python3 /path/to/pythonbot/python-bot.py
Then as usual, reload and activate the service and the service should start.
# systemctl daemon-reload
# systemctl enable --now ircbot.service
The service will automatically start when the server boots. This is the same thing as if you would have started a tmux session, then detaching the session after you start the bot. Instead, now it is just effortlessly automatic.