如何在不获取 nohup.out 的情况下使用 nohup 命令? [英] How do I use the nohup command without getting nohup.out?

查看:34
本文介绍了如何在不获取 nohup.out 的情况下使用 nohup 命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 nohup 命令时遇到问题.

I have a problem with the nohup command.

当我运行我的工作时,我有很多数据.输出 nohup.out 变得太大,我的过程变慢了.如何在不获取 nohup.out 的情况下运行此命令?

When I run my job, I have a lot of data. The output nohup.out becomes too large and my process slows down. How can I run this command without getting nohup.out?

推荐答案

nohup 命令仅写入 nohup.out,否则输出将进入终端.如果您已将命令的输出重定向到其他地方 - 包括 /dev/null - 那就是它去的地方.

The nohup command only writes to nohup.out if the output would otherwise go to the terminal. If you have redirected the output of the command somewhere else - including /dev/null - that's where it goes instead.

 nohup command >/dev/null 2>&1   # doesn't create nohup.out

如果您使用 nohup,那可能意味着您想通过在整个事情的末尾放置另一个 & 在后台运行命令:

If you're using nohup, that probably means you want to run the command in the background by putting another & on the end of the whole thing:

 nohup command >/dev/null 2>&1 & # runs in background, still doesn't create nohup.out

在 Linux 上,使用 nohup 运行作业也会自动关闭其输入.在其他系统上,特别是 BSD 和 macOS,情况并非如此,因此在后台运行时,您可能需要手动关闭输入.虽然关闭输入对 nohup.out 的创建与否没有影响,但它避免了另一个问题:如果后台进程试图从标准输入读取任何内容,它将暂停,等待您带来它回到前台并输入一些东西.所以额外安全的版本看起来像这样:

On Linux, running a job with nohup automatically closes its input as well. On other systems, notably BSD and macOS, that is not the case, so when running in the background, you might want to close input manually. While closing input has no effect on the creation or not of nohup.out, it avoids another problem: if a background process tries to read anything from standard input, it will pause, waiting for you to bring it back to the foreground and type something. So the extra-safe version looks like this:

nohup command </dev/null >/dev/null 2>&1 & # completely detached from terminal 

但是请注意,这不会阻止命令直接访问终端,也不会将其从 shell 的进程组中删除.如果您想执行后者,并且您正在运行 bash、ksh 或 zsh,您可以通过运行 disown 作为下一个命令而不带参数来实现.这意味着后台进程不再与外壳作业"相关联,并且不会从外壳向它转发任何信号.(注意区别:一个 disowned 进程不会得到它的父 shell 自动转发给它的信号 - 但如果没有 nohup,它仍然会收到一个 HUP 信号通过其他方式发送,例如手动 kill 命令.nohup'ed 进程忽略任何和所有 HUP 信号,无论它们是如何发送的.)

Note, however, that this does not prevent the command from accessing the terminal directly, nor does it remove it from your shell's process group. If you want to do the latter, and you are running bash, ksh, or zsh, you can do so by running disown with no argument as the next command. That will mean the background process is no longer associated with a shell "job" and will not have any signals forwarded to it from the shell. (Note the distinction: a disowned process gets no signals forwarded to it automatically by its parent shell - but without nohup, it will still receive a HUP signal sent via other means, such as a manual kill command. A nohup'ed process ignores any and all HUP signals, no matter how they are sent.)

说明:

在 Unixy 系统中,每个输入源或输出目标都有一个与之关联的编号,称为文件描述符",或简称为fd".每个正在运行的程序(进程")都有自己的一组这些,当一个新进程启动时,其中三个已经打开:标准输入",即 fd 0,打开供进程读取,而标准输出"(fd 1)和标准错误"(fd 2)是开放的,可以写入.如果您只是在终端窗口中运行命令,那么默认情况下,您键入的任何内容都会进入其标准输入,而其标准输出和标准错误都会发送到该窗口.

In Unixy systems, every source of input or target of output has a number associated with it called a "file descriptor", or "fd" for short. Every running program ("process") has its own set of these, and when a new process starts up it has three of them already open: "standard input", which is fd 0, is open for the process to read from, while "standard output" (fd 1) and "standard error" (fd 2) are open for it to write to. If you just run a command in a terminal window, then by default, anything you type goes to its standard input, while both its standard output and standard error get sent to that window.

但是您可以在启动命令之前要求 shell 更改任何或所有这些文件描述符指向的位置;这就是重定向 (<, <<, >, >>) 和管道 (|) 运算符可以.

But you can ask the shell to change where any or all of those file descriptors point before launching the command; that's what the redirection (<, <<, >, >>) and pipe (|) operators do.

管道是其中最简单的... command1 |command2 安排command1 的标准输出直接输入command2 的标准输入.这是一个非常方便的安排,导致了 UNIX 工具中的特定设计模式(并解释了标准错误的存在,它允许程序向用户发送消息,即使其输出正在进入管道中的下一个程序).但是您只能通过管道将标准输出传输到标准输入;你不能不做一些杂耍就将任何其他文件描述符发送到管道.

The pipe is the simplest of these... command1 | command2 arranges for the standard output of command1 to feed directly into the standard input of command2. This is a very handy arrangement that has led to a particular design pattern in UNIX tools (and explains the existence of standard error, which allows a program to send messages to the user even though its output is going into the next program in the pipeline). But you can only pipe standard output to standard input; you can't send any other file descriptors to a pipe without some juggling.

重定向操作符更友好,因为它们让您指定要重定向的文件描述符.所以 0 从名为 infile 的文件中读取标准输入,而 2>>logfile 将标准错误附加到名为<代码>日志文件.如果不指定数字,则输入重定向默认为 fd 0(<0< 相同),而输出重定向默认为 fd 1(>1> 相同).

The redirection operators are friendlier in that they let you specify which file descriptor to redirect. So 0<infile reads standard input from the file named infile, while 2>>logfile appends standard error to the end of the file named logfile. If you don't specify a number, then input redirection defaults to fd 0 (< is the same as 0<), while output redirection defaults to fd 1 (> is the same as 1>).

此外,您可以将文件描述符组合在一起:2>&1 表示将标准错误发送到标准输出的任何地方".这意味着您会得到一个包含标准输出和标准错误的单一输出流,无法再将它们分开,但这也意味着您可以在管道中包含标准错误.

Also, you can combine file descriptors together: 2>&1 means "send standard error wherever standard output is going". That means that you get a single stream of output that includes both standard out and standard error intermixed with no way to separate them anymore, but it also means that you can include standard error in a pipe.

所以序列 >/dev/null 2>&1 的意思是将标准输出发送到 /dev/null"(这是一个特殊的设备,它只是扔掉你写的任何东西)然后将标准错误发送到标准输出的任何地方"(我们刚刚确定它是/dev/null).基本上,丢弃此命令写入任一文件描述符的任何内容".

So the sequence >/dev/null 2>&1 means "send standard output to /dev/null" (which is a special device that just throws away whatever you write to it) "and then send standard error to wherever standard output is going" (which we just made sure was /dev/null). Basically, "throw away whatever this command writes to either file descriptor".

nohup 检测到它的标准错误和输出都没有附加到终端时,它不会费心去创建 nohup.out,而是假设输出是已经重定向到用户希望它去的地方.

When nohup detects that neither its standard error nor output is attached to a terminal, it doesn't bother to create nohup.out, but assumes that the output is already redirected where the user wants it to go.

/dev/null 设备也适用于输入;如果您使用 </dev/null 运行命令,则该命令从标准输入读取的任何尝试都将立即遇到文件结尾.请注意,合并语法在这里不会产生相同的效果;它只能将文件描述符指向另一个在同一方向(输入或输出)打开的文件描述符.shell 会让你做 >/dev/null <&1,但这最终会创建一个进程,在输出流上打开一个输入文件描述符,所以而不是仅仅点击结束 -of-file,任何读取尝试都会触发致命的无效文件描述符"错误.

The /dev/null device works for input, too; if you run a command with </dev/null, then any attempt by that command to read from standard input will instantly encounter end-of-file. Note that the merge syntax won't have the same effect here; it only works to point a file descriptor to another one that's open in the same direction (input or output). The shell will let you do >/dev/null <&1, but that winds up creating a process with an input file descriptor open on an output stream, so instead of just hitting end-of-file, any read attempt will trigger a fatal "invalid file descriptor" error.

这篇关于如何在不获取 nohup.out 的情况下使用 nohup 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆