Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart a python script after it finishes

Tags:

python

cron

I have a python script that will be running that basically collects data and inserts it into a database based on the last time the database was updated. Basically, i want this script to keep running and never stop, and to start up again after it finishes. What would be the best way to do this?

I considered using a cronjob and creating a lockfile and just have the script run every minute, but i feel like there may be a more effective way.

This script currently is written in python 2.7 on an ubuntu OS

Thanks for the help!

like image 435
user2146933 Avatar asked Mar 27 '14 18:03

user2146933


People also ask

How do I keep a Python script open after running?

cmd /k is the typical way to open any console application (not only Python) with a console window that will remain after the application closes. The easiest way I can think to do that, is to press Win+R, type cmd /k and then drag&drop the script you want to the Run dialog. Fantastic answer. You should have got this.

How do I completely exit a Python script?

Ctrl + C on Windows can be used to terminate Python scripts and Ctrl + Z on Unix will suspend (freeze) the execution of Python scripts. If you press CTRL + C while a script is running in the console, the script ends and raises an exception.


1 Answers

You could wrap your script in a

while True:
    ...

block, or with a bash script:

while true ; do
    yourpythonscript.py
done
like image 171
Jasper Avatar answered Sep 28 '22 10:09

Jasper