How to restart a python program from inside

Ali Satvaty
3 min readMay 17, 2021

I was developing a Telegram bot for one of my clients which was aimed to get Instagram URLs and then download public Instagram pages’ stories and posts, sending those to the users in the Telegram afterward. I used the python-telegram-bot package and also instaloader for this project. Implementation of this project is not the topic of this post and maybe I write about it in another post since I had some struggles with that and I think it can be interesting. In this post, I want to discuss one of those struggles and then share my solution for the problem with you.

Problem

To implement this bot I had to log in with an Instagram account into the instaloader. As you are probably aware, Instagram does not have any official APIs, which makes it a little bit tricky for the developers to work with that. One of these hardships which appeared in this project was that after logging into the account, sometimes Instagram logged me out of my account and I had to restart the bot. The bot was running on a VPS Linux server, therefore I had to connect to the server via SSH and then restart the python program manually. It was a little bit annoying for me, so I decided to create a button for my client in the Telegram bot to restart the bot.

My Solution

As it seems obvious, restarting a program is nothing else than at first, killing the program, and then running it again. Therefore I decided to write two other scripts: the first one for killing the program and the second one for running the program.

Interestingly, the script which kills the program (I call it killer) itself is a python program that searches among different programs that are running on the OS and then finds the PID associated with my program. Of course, at first, I wanted to just use python’s exit() function to end the program, but the thing is that the program had different threads which were running, so I needed to kill all of those.

The other script which is aimed to run the program (I call it runner)is a simple bash script that initially activates the virtualenv associated with the program and then runs the program. There is also another bash script for running the killer program.

After writing these scripts, I created a function inside the main program (which was aimed to be restarted) that after being called creates two crontabs, the first one for running the killer and the second one for running the runner bash script. This function uses a python module named python-crontab that can set and reset the crontabs of the system. Moreover, it is important to set the execution times of those crontabs properly. I set the time of the killer script for 1 minute later and the runner for 2 minutes later. This is because crontabs do not have a second determiner, and also the runner program should be run after the killer program. So we need 1 minute between running them to be sure that those will be executed respectively.

Another noteworthy notion is that after running the program, we should clear the crontabs to not allow them to run again (and kill the program).

Implementation

Code is available in the link below.

In the end, I should mention that this approach works because I name my running program such that there is only 1 python file running on the system with that name. For example, if you have two python files, both of them named main.py and both running, you cannot use this approach, because here I try to find PIDs of the threads using the name of the file which is running.

Conclusion

Finally, I should mention that this is probably not the perfect way to do this and maybe there are better approaches, but I was low on time and I needed to just discover a solution. Therefore feel free to suggest and share your ideas with me on what are the other solutions.

Happy coding and drinking coffee!

--

--