Skip to content

Instantly share code, notes, and snippets.

@ehoppmann
Created June 25, 2020 15:58
Show Gist options
  • Save ehoppmann/280d808c8c5e9a9a9097d7d800b27c4f to your computer and use it in GitHub Desktop.
Save ehoppmann/280d808c8c5e9a9a9097d7d800b27c4f to your computer and use it in GitHub Desktop.
A python script that restarts itself (running a new process that reflects any changes on disk)
#!/usr/bin/env python3
# based on https://github.com/cherrypy/cherrypy/blob/0857fa81eb0ab647c7b59a019338bab057f7748b/cherrypy/process/wspbus.py#L305
import sys
import os
import time
_startup_cwd = os.getcwd()
def _do_execv():
args = sys.argv[:]
print('Re-spawning %s' % ' '.join(args))
args.insert(0, sys.executable)
if sys.platform == 'win32':
args = ['"%s"' % arg for arg in args]
os.chdir(_startup_cwd)
os.execv(sys.executable, args)
if __name__ == '__main__':
start_time = time.time()
while True:
td = (time.time() - start_time)
if td > 5:
print('_do_execv')
_do_execv()
exit(0)
print(f'{td}')
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment