达尔文:如何在不杀死子进程的情况下杀死父进程?

发布于 2024-08-16 01:18:01 字数 81 浏览 9 评论 0原文

在 OS X 10.4/5/6 上:

我有一个生成子进程的父进程。我想杀死父母而不杀死孩子。是否可以?我可以修改任一应用程序上的源。

On OS X 10.4/5/6:

I have a parent process which spawns a child. I want to kill the parent without killing the child. Is it possible? I can modify source on either app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油焖大侠 2024-08-23 01:18:01

正如 NSD 所问的,这实际上取决于它是如何产生的。例如,如果您使用的是 shell 脚本,则可以使用 nohup 命令来运行子进程。

如果您使用的是fork/exec,那么它会稍微复杂一些,但也不会太复杂。

来自 http://code.activestate.com/recipes/66012/

import sys, os 

def main():
    """ A demo daemon main routine, write a datestamp to 
        /tmp/daemon-log every 10 seconds.
    """
    import time

    f = open("/tmp/daemon-log", "w") 
    while 1: 
        f.write('%s\n' % time.ctime(time.time())) 
        f.flush() 
        time.sleep(10) 


if __name__ == "__main__":
    # do the UNIX double-fork magic, see Stevens' "Advanced 
    # Programming in the UNIX Environment" for details (ISBN 0201563177)
    try: 
        pid = os.fork() 
        if pid > 0:
            # exit first parent
            sys.exit(0) 
    except OSError, e: 
        print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror) 
        sys.exit(1)

    # decouple from parent environment
    os.chdir("/") 
    os.setsid() 
    os.umask(0) 

    # do second fork
    try: 
        pid = os.fork() 
        if pid > 0:
            # exit from second parent, print eventual PID before
            print "Daemon PID %d" % pid 
            sys.exit(0) 
    except OSError, e: 
        print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror) 
        sys.exit(1) 

    # start the daemon main loop
    main() 

这是其中之一有史以来最好的书。它非常详细地涵盖了这些主题。

UNIX 环境中的高级编程,第二版(Addison-Wesley 专业计算系列)(平装本)

ISBN-10: 0321525949
ISBN-13:978-0321525949

5 星亚马逊评论(我会给它 6 分)。

As NSD asked, it really depends on how it is spawned. If you are using a shell script, for example, you can use the nohup command to run the child through.

If you are using fork/exec, then it is a little more complicated, but no too much so.

From http://code.activestate.com/recipes/66012/

import sys, os 

def main():
    """ A demo daemon main routine, write a datestamp to 
        /tmp/daemon-log every 10 seconds.
    """
    import time

    f = open("/tmp/daemon-log", "w") 
    while 1: 
        f.write('%s\n' % time.ctime(time.time())) 
        f.flush() 
        time.sleep(10) 


if __name__ == "__main__":
    # do the UNIX double-fork magic, see Stevens' "Advanced 
    # Programming in the UNIX Environment" for details (ISBN 0201563177)
    try: 
        pid = os.fork() 
        if pid > 0:
            # exit first parent
            sys.exit(0) 
    except OSError, e: 
        print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror) 
        sys.exit(1)

    # decouple from parent environment
    os.chdir("/") 
    os.setsid() 
    os.umask(0) 

    # do second fork
    try: 
        pid = os.fork() 
        if pid > 0:
            # exit from second parent, print eventual PID before
            print "Daemon PID %d" % pid 
            sys.exit(0) 
    except OSError, e: 
        print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror) 
        sys.exit(1) 

    # start the daemon main loop
    main() 

This is one of the best books ever written. It covers these topics in great and extensive detail.

Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series) (Paperback)

ISBN-10: 0321525949
ISBN-13: 978-0321525949

5 star amazon reviews (I'd give it 6).

陌上芳菲 2024-08-23 01:18:01

如果父级是 shell,并且您想要启动长时间运行的进程然后注销,请考虑 nohup (1)disown

如果您控制子级的编码,则可以捕获 SIGHUP 并以某种非默认方式处理它(例如完全忽略它)。请阅读 signal (3)sigaction (2) 手册页以获取相关帮助。无论哪种方式,StackOverflow 上都有几个现有的问题,并提供了很好的帮助。

If the parent is a shell, and you want to launch a long running process then logout, consider nohup (1) or disown.

If you control the coding of the child, you can trap SIGHUP and handling it in some non-default way (like ignoring it outright). Read the signal (3) and sigaction (2) man pages for help with this. Either way there are several existing questions on StackOverflow with good help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文