swabber-strace-attach 609 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env python
  2. import os
  3. import sys
  4. # Detach from the controlling terminal and parent process by forking twice to daemonize ourselves,
  5. # then run the command passed as argv[1]. Send log data to argv[2].
  6. pid = os.fork()
  7. if (pid == 0):
  8. os.setsid()
  9. pid = os.fork()
  10. if (pid != 0):
  11. os._exit(0)
  12. else:
  13. sys.exit()
  14. si = file(os.devnull, 'r')
  15. so = file(sys.argv[2], 'w')
  16. se = so
  17. # Replace those fds with our own
  18. os.dup2(si.fileno(), sys.stdin.fileno())
  19. os.dup2(so.fileno(), sys.stdout.fileno())
  20. os.dup2(se.fileno(), sys.stderr.fileno())
  21. ret = os.system(sys.argv[1])
  22. os._exit(ret)