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

问题描述

我的nohup命令有问题.

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

解决方案

如果输出否则将输出到终端,则nohup命令仅写入nohup.out.如果您已将命令的输出重定向到其他位置(包括/dev/null),那么它将在那里.

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

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

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

在Linux上,使用nohup运行作业也会自动关闭其输入.在其他系统上,尤其是在BSD和macOS上,则不是这种情况,因此,在后台运行时,您可能需要手动关闭输入.虽然关闭输入对nohup.out的创建或不起作用没有影响,但它避免了另一个问题:如果后台进程尝试从标准输入中读取任何内容,它将暂停,等待您将其放回前台并键入内容.因此,超安全版本如下所示:

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

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

说明:

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

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

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

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

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

因此序列>/dev/null 2>&1的意思是将标准输出发送到/dev/null"(这是一种特殊的设备,只会丢弃您写入的内容)然后将标准错误发送到标准输出所要到达的任何地方"(我们只是确定是/dev/null).基本上,丢弃此命令写入到任一文件描述符中的任何内容".

nohup未检测到其标准错误或输出未附加到终端时,它不会费心创建nohup.out,而是假定输出已被重定向到用户想要的位置.

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

I have a problem with the nohup command.

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?

解决方案

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

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

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

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.)

Explanation:

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.

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.

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.

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>).

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.

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".

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.

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命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 08:20