In this tutorial you will learn how to run "something" (e.g. a file or a script) 24/7 on Debian and Ubuntu servers and start it on boot.
For the task described in this tutorial, I will use a Python script.
I am doing this on a Debian 11 server (netcup RS 2000), but other Debian versions and Ubuntu should also work.
You need a Debian or Ubuntu server with an internet connection.
You also need root permissions on your server.
First you need to connect to your server via SSH.
Then switch to the root user and update your system, followed by the installation of Cron and Python:
su
apt update && apt upgrade -y && apt autoremove -y
apt install wget curl sudo nano python-pip-whl cron python3 python3-pip screen -y
In my example I use a Python script in /home/python
which is named run.py
. I want to start it on boot and it should run 24/7.
First, I switch into the folder and install the Python requirements that I need for the script:
cd /home/python
pip3 install py-cord py-cord[voice]
Then I proceed to start the script (replace discord-py-bot
with a name and python3 run.py
with the command to start):
screen -AmdS discord-py-bot python3 run.py
We use Gnu Screen to keep the script running even if the SSH connection is closed.
If you want to see the output of the console, run the following command (replace discord-py-bot
with the name you defined in Step 1.1):
screen -r discord-py-bot
If you want to return to your normal console, press CTRL + A
and then CTRL + D
.
If you want the file to start on boot, run the following command (replace screen -AmdS discord-py-bot python3 run.py
with the entire start command of Step 1.1 and /home/python
with the folder path of your file):
{ crontab -l 2>/dev/null; echo "@reboot sleep 10 && cd /home/python && screen -AmdS discord-py-bot python3 run.py" ; } | crontab -
If you want to test whether your setup works, reboot your server:
reboot
After rebooting, check if you can see the console (replace discord-py-bot
with the name you defined in Step 1.1):
screen -r discord-py-bot
Now, every time you reboot your server, the file (in our example, the Python file) is also started and executed in the background.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Origin By making a contribution to this project, I certify that:
The contribution was created in whole or in part by me and I have the right to submit it under the license indicated in the file; or
The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same license (unless I am permitted to submit under a different license), as indicated in the file; or
The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.
I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the license(s) involved.