Skip to content

Instantly share code, notes, and snippets.

@brothertao
Created February 8, 2023 12:15
Show Gist options
  • Save brothertao/44d8800edebba838903d77a8d12c94a5 to your computer and use it in GitHub Desktop.
Save brothertao/44d8800edebba838903d77a8d12c94a5 to your computer and use it in GitHub Desktop.
监护进程:如果非正常退出自动,重新拉起
#!/usr/bin/env python3
import subprocess as sp
from datetime import datetime
import sys
lasttime = datetime.now()
cnt = 0
argv = sys.argv[1:]
p = sp.run(argv)
while(True):
cnt += 1
print(f'重启次数:{cnt}')
if p.returncode == 0:
exit(0)
if cnt%5 == 0:
now = datetime.now()
delta = now - lasttime
if delta.seconds < 10:
print('频繁重启,不再守护')
exit(5)
lasttime = now
p = sp.run(argv)
'''
#### dev
which env
chmod +x supervisor1.py
cd .
python3 supervisor1.py ls -alh
./supervisor1.py curl ba
./supervisor1.py sleep 1000
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment