Skip to content

Instantly share code, notes, and snippets.

@gauravssnl
Created September 4, 2021 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauravssnl/481d25069153a6631382806b79afba57 to your computer and use it in GitHub Desktop.
Save gauravssnl/481d25069153a6631382806b79afba57 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Waits for the parent process to terminate, then executes specified commands.
import os
import signal
import sys
import syslog
import time
if len(sys.argv) < 3:
raise Exception('usage: restart.py <pid> <path> [optional command]')
try:
signal.signal(signal.SIGHUP, signal.SIG_IGN)
pid = int(sys.argv[1])
while os.getppid() == pid:
time.sleep(0.5)
if len(sys.argv) > 3:
to_launch = sys.argv[3:]
ec = os.spawnv(os.P_WAIT, to_launch[0], to_launch)
if ec != 0:
syslog.syslog(syslog.LOG_ERR, str(to_launch) + ': ' + str(ec))
to_launch = ['/usr/bin/open', sys.argv[2]] if sys.platform == 'darwin' else [sys.argv[2]]
os.execv(to_launch[0], to_launch)
except:
syslog.syslog(syslog.LOG_ERR, str(sys.exc_info()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment