当前位置:网站首页>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));
边栏推荐
猜你喜欢
OneManager搭建
【编程题】【Scratch三级】2022.03 冬天下雪了
DirectExchange交换机简单入门demo
Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
深度解析 z-index
mysql索引失效的常见9种原因详解
外贸网站优化-外贸网站优化教程-外贸网站优化软件
11.0 堆参数调优入门之堆参数调整
SSH远程管理
浅层了解欧拉函数
随机推荐
Zabbix入门
JDBC的使用
Oracle入门 05 - VirtualBox的虚拟机安装配置
shell的脚本的基本用法
ls的用法
cp 的用法
性能测试概述
MySQL系列一:账号管理与引擎
How to choose a suitable UI component library in uni-app
Hook API
JS函数柯里化
运行 npm 会弹出询问 “你要如何打开这个文件?“
Oracle入门 12 - Linux 磁盘分区及LVM实战
数据库原理作业3 — JMU
routeros KVM安装LEDE 20191030最新版应用
自动翻译软件-批量批量自动翻译软件推荐
DDNS搭建
测试——用例篇
SSH远程管理
编辑时过滤当前节点及根据限制的层数过滤数据