Mercurial > hg > config
view python/attach.py @ 694:ebca6d85213a
File "/usr/lib/python3/dist-packages/IPython/config/__init__.py", line 16, in <module>
from .application import *
File "/usr/lib/python3/dist-packages/IPython/config/application.py", line 31, in <module>
from IPython.config.configurable import SingletonConfigurable
File "/usr/lib/python3/dist-packages/IPython/config/configurable.py", line 33, in <module>
from IPython.utils.text import indent, wrap_paragraphs
File "/usr/lib/python3/dist-packages/IPython/utils/text.py", line 28, in <module>
from IPython.external.path import path
File "/usr/lib/python3/dist-packages/IPython/external/path/__init__.py", line 2, in <module>
from path import *
File "/home/jhammel/python/path.py", line 25
print root(path)
^
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 09 Jul 2014 16:26:49 -0700 |
parents | 72f33eb3aa23 |
children |
line wrap: on
line source
#!/usr/bin/env python """ attach strace to a process: 624 sudo strace -p $(pidof firefox) 625 kill 6976 626 isrunning firefox Optionally kill the process when detached """ import optparse import os import subprocess import sys from subprocess import check_output def main(args=sys.argv[1:]): usage = '%prog [options] ProgramName_or_PID' parser = optparse.OptionParser(usage=usage, description=__doc__) parser.add_option('-k', '--kill', action='store_true', default=False, help="kill process after strace is done") options, args = parser.parse_args(args) if len(args) != 1: parser.print_usage() parser.error("Please specify program or PID to attach to") # get the PID try: pid = int(args[0]) except ValueError: pid = check_output(["pidof", args[0]]).strip().split() if len(pid) > 1: parser.error("Multiple PIDs found for %s:\n%s" % (args[0], '\n'.join(pid))) # TODO: handle not matching case pid = int(pid[0]) # invoke strace subprocess.call(['sudo', 'strace', '-p', str(pid)]) # kill if specified os.kill(pid, 9) # TODO: better than SIGKILL if __name__ == '__main__': main()