我用nohup命令有问题。

当我运行我的工作时,我有很多数据。输出nohup。Out变得太大,我的进程变慢了。我如何运行这个命令而不得到noup .out?


当前回答

你可能需要使用分离程序。您可以像nohup一样使用它,但是它不会产生输出日志,除非您要求它这样做。下面是手册页:

NAME
       detach - run a command after detaching from the terminal

SYNOPSIS
       detach [options] [--] command [args]

       Forks  a  new process, detaches is from the terminal, and executes com‐
       mand with the specified arguments.

OPTIONS
       detach recognizes a couple of options, which are discussed below.   The
       special  option -- is used to signal that the rest of the arguments are
       the command and args to be passed to it.

       -e file
              Connect file to the standard error of the command.

       -f     Run in the foreground (do not fork).

       -i file
              Connect file to the standard input of the command.

       -o file
              Connect file to the standard output of the command.

       -p file
              Write the pid of the detached process to file.

EXAMPLE
       detach xterm

       Start an xterm that will not be closed when the current shell exits.

AUTHOR
       detach was written by Robbert Haarman.  See  http://inglorion.net/  for
       contact information.

注意,我与该程序的作者没有任何关系。我只是一个满意的程序用户。

其他回答

你可能需要使用分离程序。您可以像nohup一样使用它,但是它不会产生输出日志,除非您要求它这样做。下面是手册页:

NAME
       detach - run a command after detaching from the terminal

SYNOPSIS
       detach [options] [--] command [args]

       Forks  a  new process, detaches is from the terminal, and executes com‐
       mand with the specified arguments.

OPTIONS
       detach recognizes a couple of options, which are discussed below.   The
       special  option -- is used to signal that the rest of the arguments are
       the command and args to be passed to it.

       -e file
              Connect file to the standard error of the command.

       -f     Run in the foreground (do not fork).

       -i file
              Connect file to the standard input of the command.

       -o file
              Connect file to the standard output of the command.

       -p file
              Write the pid of the detached process to file.

EXAMPLE
       detach xterm

       Start an xterm that will not be closed when the current shell exits.

AUTHOR
       detach was written by Robbert Haarman.  See  http://inglorion.net/  for
       contact information.

注意,我与该程序的作者没有任何关系。我只是一个满意的程序用户。

sudo bash -c "nohup /opt/viptel/viptel_bin/log.sh $* &> /dev/null"  &

重定向sudo的输出会导致sudo重新询问密码,因此需要一种笨拙的机制来执行这种变体。

你试过重定向所有三个I/O流吗:

nohup ./yourprogram > foo.out 2> foo.err < /dev/null &

nohup命令只写nohup。如果输出本来要到终端,则退出。如果您已经将命令的输出重定向到其他地方——包括/dev/null——那么它将转到其他地方。

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

注意,在大多数(但不是所有)shell中,>/dev/null 2>&1序列可以缩写为>&/dev/null。

如果你正在使用nohup,这可能意味着你想在后台运行命令,在整个命令的末尾加上另一个&:

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

解释:

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更改任何或所有这些文件描述符指向的位置;这就是重定向(<,<<,>,>>)和管道(|)操作符所做的。

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从名为infile的文件中读取标准输入,而2>>logfile将标准错误追加到名为logfile的文件的末尾。如果不指定数字,则输入重定向默认为fd 0(<与0<相同),而输出重定向默认为fd 1(>与1>相同)。

同样,你可以将文件描述符组合在一起:2>&1表示“无论标准输出在哪里,都发送标准错误”。这意味着您将得到一个包含标准输出和标准错误的单一输出流,并且无法再将它们分开,但这也意味着您可以在管道中包含标准错误。

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

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

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 <your command> & >  <outputfile> 2>&1 &

如。 我在脚本里面有一个nohup命令

./Runjob.sh > sparkConcuurent.out 2>&1