- 
      
 - 
        
Save ibigbug/5b819303f4aa625b8f24615fabd1b567 to your computer and use it in GitHub Desktop.  
    tee daemon won't exit
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/bin/bash | |
| main() { | |
| echo 1 | |
| python sig.py & | |
| echo 2 | |
| } | |
| main 2>&1 | tee -a a.log | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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