swabber-strace-attach 645 B

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