Skip to content

Instantly share code, notes, and snippets.

@ibigbug
Created June 29, 2016 13:25
Show Gist options
  • Save ibigbug/5b819303f4aa625b8f24615fabd1b567 to your computer and use it in GitHub Desktop.
Save ibigbug/5b819303f4aa625b8f24615fabd1b567 to your computer and use it in GitHub Desktop.
tee daemon won't exit
#!/bin/bash
main() {
echo 1
python sig.py &
echo 2
}
main 2>&1 | tee -a a.log
import os
import sys
import signal
def handle(signum, frame):
if signum in [1, 2, 3, 15]:
print('exiting with sig: %s' % signum)
sys.exit()
else:
print('sig %s, ignore' % signum)
def main():
bypass = ['SIG_DFL', 'SIGKILL', 'SIGSTOP']
for i in [x for x in dir(signal) if x.startswith('SIG')]:
if not i in bypass:
signum = getattr(signal, i)
signal.signal(signum, handle)
print(signum, i)
print('pid: %s, ppid: %s' % (os.getpid(), os.getppid()))
while 1: pass
if __name__ == '__main__':
try:
pid = os.fork()
if pid > 0:
sys.exit(0)
except OSError, e:
print >> sys.stderr, 'fork #1 failed: %d (%s)' % (e.errno, e.stderror)
os.chdir('/')
os.setsid()
os.umask(0)
try:
pid = os.fork()
if pid > 0:
print 'daemon PID %d' % pid
sys.exit(0)
except OSError, e:
print >>sys.stderr, 'fork #2 failed: %d (%s)' % (e.errno, e.stderror)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment