当前位置:网站首页>nohup原理
nohup原理
2022-07-31 05:38:00 【Hermokrates】
nohup
nohup 执行会忽略信号 SIGHUP
,并将 stdout/stderr
重定向到文件 nohup.out
。以便shell在关闭或注销后命令可以在后台继续运行 。nohup做的工作就是让 nohup 后的命令不在是当前 shell 的子命令。而是PPID=1的进程(进程的PPID=1)。这种情况下不能被带回到前台。
signal (SIGHUP, SIG_IGN); // 忽略信号SIGHUP
char **cmd = argv + optind;
execvp (*cmd, cmd); // 在执行这个命令,而不是当前shell
对于输出的重定向,对于STDOUT/STDERR会忽略,然后写入到 nohup.out
ignoring_input = isatty (STDIN_FILENO);
redirecting_stdout = isatty (STDOUT_FILENO);
stdout_is_closed = (!redirecting_stdout && errno == EBADF);
redirecting_stderr = isatty (STDERR_FILENO);
/* If standard input is a tty, replace it with /dev/null if possible. Note that it is deliberately opened for *writing*, to ensure any read evokes an error. */
if (ignoring_input)
{
if (fd_reopen (STDIN_FILENO, "/dev/null", O_WRONLY, 0) < 0)
error (exit_internal_failure, errno,
_("failed to render standard input unusable"));
if (!redirecting_stdout && !redirecting_stderr)
error (0, 0, _("ignoring input"));
}
/* If standard output is a tty, redirect it (appending) to a file. First try nohup.out, then $HOME/nohup.out. If standard error is a tty and standard output is closed, open nohup.out or $HOME/nohup.out without redirecting anything. */
if (redirecting_stdout || (redirecting_stderr && stdout_is_closed))
{
char *in_home = NULL;
char const *file = "nohup.out";
int flags = O_CREAT | O_WRONLY | O_APPEND;
mode_t mode = S_IRUSR | S_IWUSR;
mode_t umask_value = umask (~mode);
out_fd = (redirecting_stdout
? fd_reopen (STDOUT_FILENO, file, flags, mode)
: open (file, flags, mode));
边栏推荐
猜你喜欢
随机推荐
vmware搭建redis集群遇到问题
Dart入门
浅层了解欧拉函数
使用powerDesigner反向工程生成Entity
【云原生】3.3 Kubernetes 中间件部署实战
Gradle剔除依赖演示
webdriver.定位元素
OSI七层模型
小实战项目之——吃货联盟订餐系统
DNS域名解析服务
机器学习反向传播的一些推导公式
FRP穿透教程
In-depth analysis of z-index
测试——用例篇
codec2 BlockPool:unreadable libraries
Third-party library-store
Oracle入门 04 - Vmware虚拟机安装配置
HuffmanTree
Oracle入门 03 - Linux / Unix 系统基础知识
11.0 堆参数调优入门之堆参数调整