免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6566 | 回复: 4
打印 上一主题 下一主题

求大侠nohup命令详解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-01 19:53 |只看该作者 |倒序浏览
求问nohup 将命令放到后台后,重启终端,能否调出将之到终端?如何调入?

二:用nohup后,文件大小没变了,top查看程序在运行,也没出现nohupout文件.......

论坛徽章:
3
CU十二周年纪念徽章
日期:2013-10-24 15:41:34狮子座
日期:2014-03-27 15:44:382015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2011-11-01 20:21 |只看该作者
你使用ps aux|grep nohupout 查看有没有

都放到后台了 拿不会来了吧

论坛徽章:
0
3 [报告]
发表于 2011-11-02 00:30 |只看该作者
回复 2# cu_little_bird


    谢谢 。。。

论坛徽章:
1
天秤座
日期:2013-10-23 13:20:42
4 [报告]
发表于 2011-11-02 10:54 |只看该作者
"求问nohup 将命令放到后台后"
nohup不是将命令/进程放到后台,&才是启动这个作用的

nohup - run a command immune to hangups, with output to a non-tty

这才是nohup真正的作用

论坛徽章:
0
5 [报告]
发表于 2011-11-11 15:37 |只看该作者
本帖最后由 可可火山 于 2011-11-11 15:41 编辑

Read the fucking source code.
nohup是忽略 SIGHUP信号,这个信号在终端断开等情况下触发,比如当你ssh断开远程连接,就会触发这个信号,默认情况下程序对这个信号的处理就是退出。


不过我想知道 solaris zone环境下的nohup是否有区别,曾有个问题研究了下nohup源代码,不过没找到我生成环境nohup的源代码。
  1. /*
  2. * Copyright (c) 1989, 1993
  3. *        The Regents of the University of California.  All rights reserved.
  4. * Portions copyright (c) 2007 Apple Inc.  All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. *    notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. *    notice, this list of conditions and the following disclaimer in the
  13. *    documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. *    may be used to endorse or promote products derived from this software
  16. *    without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */

  30. #if 0
  31. #ifndef lint
  32. static const char copyright[] =
  33. "@(#) Copyright (c) 1989, 1993\n\
  34.         The Regents of the University of California.  All rights reserved.\n";
  35. #endif /* not lint */

  36. #ifndef lint
  37. static char sccsid[] = "@(#)nohup.c        8.1 (Berkeley) 6/6/93";
  38. #endif /* not lint */
  39. #endif
  40. #include <sys/cdefs.h>
  41. #ifndef __APPLE__
  42. __FBSDID("$FreeBSD: src/usr.bin/nohup/nohup.c,v 1.10 2003/05/03 19:44:46 obrien Exp $");
  43. #endif

  44. #include <sys/param.h>
  45. #include <sys/stat.h>

  46. #include <err.h>
  47. #include <errno.h>
  48. #include <fcntl.h>
  49. #include <signal.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <unistd.h>

  54. #ifdef __APPLE__
  55. #include <vproc.h>
  56. #include <vproc_priv.h>
  57. #endif

  58. static void dofile(void);
  59. static void usage(void);

  60. #define        FILENAME        "nohup.out"
  61. /*
  62. * POSIX mandates that we exit with:
  63. * 126 - If the utility was found, but failed to execute.
  64. * 127 - If any other error occurred.
  65. */
  66. #define        EXIT_NOEXEC        126
  67. #define        EXIT_NOTFOUND        127
  68. #define        EXIT_MISC        127

  69. int
  70. main(int argc, char *argv[])
  71. {
  72.         int exit_status;

  73.         while (getopt(argc, argv, "") != -1)
  74.                 usage();
  75.         argc -= optind;
  76.         argv += optind;
  77.         if (argc < 1)
  78.                 usage();

  79.         if (isatty(STDOUT_FILENO))
  80.                 dofile();
  81.         if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
  82.                 /* may have just closed stderr */
  83.                 err(EXIT_MISC, "%s", argv[0]);

  84.         (void)signal(SIGHUP, SIG_IGN);

  85. #ifdef __APPLE__
  86.         if (_vprocmgr_move_subset_to_user(geteuid(), "Background") != NULL)
  87.                 err(EXIT_MISC, "can't migrate to background session");
  88. #endif
  89.         execvp(*argv, argv);
  90.         exit_status = (errno == ENOENT) ? EXIT_NOTFOUND : EXIT_NOEXEC;
  91.         err(exit_status, "%s", argv[0]);
  92. }

  93. static void
  94. dofile(void)
  95. {
  96.         int fd;
  97.         char path[MAXPATHLEN];
  98.         const char *p;

  99.         /*
  100.          * POSIX mandates if the standard output is a terminal, the standard
  101.          * output is appended to nohup.out in the working directory.  Failing
  102.          * that, it will be appended to nohup.out in the directory obtained
  103.          * from the HOME environment variable.  If file creation is required,
  104.          * the mode_t is set to S_IRUSR | S_IWUSR.
  105.          */
  106.         p = FILENAME;
  107.         fd = open(p, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
  108.         if (fd != -1)
  109.                 goto dupit;
  110.         if ((p = getenv("HOME")) != NULL && *p != '\0' &&
  111.             (size_t)snprintf(path, sizeof(path), "%s/%s", p, FILENAME) <
  112.             sizeof(path)) {
  113.                 fd = open(p = path, O_RDWR | O_CREAT | O_APPEND,
  114.                     S_IRUSR | S_IWUSR);
  115.                 if (fd != -1)
  116.                         goto dupit;
  117.         }
  118.         errx(EXIT_MISC, "can't open a nohup.out file");

  119. dupit:
  120. #ifdef __APPLE__
  121.         (void)lseek(fd, 0L, SEEK_END);
  122. #endif
  123.         if (dup2(fd, STDOUT_FILENO) == -1)
  124.                 err(EXIT_MISC, NULL);
  125.         (void)fprintf(stderr, "appending output to %s\n", p);
  126. }

  127. static void
  128. usage(void)
  129. {
  130.         (void)fprintf(stderr, "usage: nohup [--] utility [arguments]\n");
  131.         exit(EXIT_MISC);
  132. }
复制代码
FreeBSD源码学习——nohup命令源程序分析
(2005-10-31 16:04:0

nohup命令是最长用到的几个命令之一,看了一下他的源码,真没想到内部执行过程原来是如此的简单 ho...ho......

step 1:
if (argc < 1)
                usage();
判断参数个数如果小于1,则显示帮助并退出。

step 2:
if (isatty(STDOUT_FILENO))
                dofile();
判断标准输出是否为终端机,如果是则执行dofile自定义函数。对于使用nohup命令后跟被执行的命令后没有提供将信息输出指向到其他设备或文件时,isatty(STDOUT_FILENO)将返回1,并执行dofile()。

step 3:
static void dofile(void)
这个自定义函数完成的功能是使用getenv("HOME"获取路径并以 #define FILENAME "nohup.out" 中定义的FILENAME为文件名,创建程序执行输出信息存放的文件句柄,并使用dup2(fd, STDOUT_FILENO)将标准输出句柄复制到此文件句柄上,这样被执行程序本应该默认将信息输出到标准输出设备上的内容就转到默认的nohup.out文件里了。

step 4:
if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
                err(EXIT_MISC, "%s", argv[0]);
判断标准错误输出是否为终端机,如果是,并且将标准错误输出句柄复制到标准输出句柄上,为了将错误信息直接输出到标准输出终端,这两步有一步操作失败则打印错误信息。

step 5:
(void)signal(SIGHUP, SIG_IGN);
屏蔽连接中断信号,以防止由于客户端连接中断造成nohup执行的程序无法完成。因为此语句的存在才让nohup命令具有实际意义。

step 6:
execvp(*argv, argv);
会从PATH环境变量所指的目录中查找符合nohup参数所指定的执行程序,找到后便执行该文件,然后将第二个参数argv传给该欲执行的文件。执行失败则直接返回-1,失败原因存于errno中。

step 7:
exit_status = (errno == ENOENT) ? EXIT_NOTFOUND : EXIT_NOEXEC;
err(exit_status, "%s", argv[0]);
execvp执行后如果失败,只有两种可能,一种是被执行的程序不存在,另一种是不能执行,因此程序只判断了这两种状态,并当在发生的时候显示错误信息。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP