Setting up auto-restart
PM2 is a process manager originally intended for Node.js but can also be used with Python applications, such as our Modmail bot and Logviewer. To use
pm2, we will need to install Node Package Manager (
sudo apt install npm -y && sudo npm i pm2 -g
sudo dnf -y install npm && sudo npm i pm2 -g
Then, in the Modmail folder, start the Modmail process in the background with:
pm2 start modmail.sh --name "modmail"
You can see the logs of your Modmail process with:
pm2 logs modmail
And then, to make sure that
pm2stays active and persistent between machine restarts, run the following commands:
pm2 save && pm2 startup
Here's some of the other PM2 commands for future reference:
pm2 restart modmail
pm2 reload modmail
pm2 stop modmail
pm2 delete modmail
systemdis a built-in service manager for most Linux systems. It's primary used to manage background applications and services and to make applications auto-restart on crash and run on system startup.
We will be using
systemdfor Modmail by making a service file for our bot.
In order to create the service file, you will first need to know three things, your Linux
username, your Modmail folder location as
modmail_pathand your Pipenv location as
First, your Linux
usernamecan be fetched with the following command:
You can get your
nano, create a service file for systemd with:
sudo nano /etc/systemd/system/modmail.service
and paste in the contents below, replacing
pipenv_pathwith yours respectively.
Ctrl+Xto exit the nano editor.
User=username # replace this
Group=username # replace this
WorkingDirectory=modmail_path # replace this
ExecStart=pipenv_path run python bot.py # replace pipenv_path only
Now, start your Modmail bot with:
sudo systemctl start modmail
If everything goes correctly, you should see your bot online. You can also view the logs of your systemd process with:
sudo journalctl -eu modmail
With that said, go ahead and enable your Modmail service to auto-restart after crash and reboot with:
sudo systemctl enable modmail
If in the future you need to stop and disable your Modmail service, you can do so with:
sudo systemctl stop modmail
sudo systemctl disable modmail
To have the bot auto-restart on crash or system reboot, we will be using
nssmby making a service for our bot application.
First, find the Python path of your Modmail pipenv by running
which pythonin your Modmail folder. Copy the path that appears in your terminal and paste it in the first line of our next step.
Second, create a file named
modmail.batin your modmail directory with the following contents, replacing
python_pathwith the one you copied previously and
call python bot.py
The finished file should look something like this:
call python bot.py
The download will be a
.zipfile so you'll need to extract it first using your file archiver program (such as WinRAR or 7-zip). After that, find
nssm.exein the folder corresponding to your OS bit version (these days it should be win64) and copy it's file path:
nssmitself is only a command-line program, we'll need to use our trusty Terminal to use the application to create our service. So, search up "Powershell" in your start menu, right-click it and click "Run as Administrator".
Change directory (CD) into the folder path that you copied earlier, the command should look like something like this:
Wrapping "your folder\directory" on Windows in double quotes is necessary to make sure spaces in our file path is parsed correctly.
And then, proceed to create a new service for Modmail using
.\nssm install "Modmail"
A GUI will pop up where you can fill in the details needed for your Modmail service. Replace the
Pathwith the path of your
modmail.batscript and the
Startup directorywith the path of your Modmail folder as follow:
You can fill in these extra details as you see fit as it's only for your own reference:
You can also optionally specify a log file as output and error in the
I/Otab, just be sure to create the file beforehand so you can select it in the GUI.
And finally, click "Install Service" to install your Modmail bot as a service on your Windows system.
By now you should have the service installed but not yet running. You can start it by using this command below in the open
nssmTerminal from earlier:
.\nssm start modmail
You should be able to see your bot running if everything goes well. You can also verify the status of your Modmail service with:
.\nssm status modmail
And if you specified the log output file in your previous step, you should be able to see your current Modmail logs like so:
And that's it! Your bot will now auto-start everytime you reboot your system. You can also additionally stop and restart your service with
.\nssm stop modmailand
.\nssm restart modmailrespectively. Refer to NSSM Documentation for further customization as this guide is only meant to cover the basic needs adequate for standard Modmail usage.