Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Python script persistently on server

I have a python bot which I'm hosting on digital ocean. Right now to run it persistently I'm using virtual terminal screen and I see that this way is far from stable.

How I can schedule my bot script to run forever and being re-run if it crashes ? And also how I can schedule some other python scripts to run once in an hour ?

like image 787
Roma Avatar asked Jan 17 '26 20:01

Roma


1 Answers

Question 1. How to make things run forever.

There are a few ways to do this. You can create system service like systemd service (tutorial here), or you can use something like supervisord to do it. Both ways can help you restart on failure.

Lastly, you can still do it with either screen or tmux, but it won't be able to auto-restart on failure.


Question 2. How to run scripts periodically?

For this, there's a built-in program called cron. You can use it to automate triggering of any script, any time you specify.

DigitalOcean: How to use cron to automate

tldr; you can just do crontab -e then add in the following

0 * * * * python /path/to/script.py

That will trigger the command on 0 minute of every hour of every day.

Here's a website which can be used to craft cron time format. https://crontab.guru/

like image 150
thuyein Avatar answered Jan 20 '26 09:01

thuyein