我用nohup命令有问题。
当我运行我的工作时,我有很多数据。输出nohup。Out变得太大,我的进程变慢了。我如何运行这个命令而不得到noup .out?
我用nohup命令有问题。
当我运行我的工作时,我有很多数据。输出nohup。Out变得太大,我的进程变慢了。我如何运行这个命令而不得到noup .out?
当前回答
如果你面前的mac/linux上有一个BASH shell,你可以尝试以下步骤来实际理解重定向:
创建一个名为zz.sh的2行脚本
#!/bin/bash
echo "Hello. This is a proper command"
junk_errorcommand
echo命令的输出进入STDOUT文件流(文件描述符1)。 error命令的输出进入STDERR文件流(文件描述符2)。
目前,只需执行脚本即可将STDOUT和STDERR发送到屏幕。
./zz.sh
现在从标准重定向开始:
zz.sh > zfile.txt
在上面,“echo”(STDOUT)进入zfile.txt。而“错误”(STDERR)显示在屏幕上。
以上同:
zz.sh 1> zfile.txt
现在您可以尝试相反的方法,将“error”STDERR重定向到文件中。来自“echo”命令的STDOUT显示在屏幕上。
zz.sh 2> zfile.txt
将上述两者结合起来,你会得到:
zz.sh 1> zfile.txt 2>&1
解释:
首先,发送stdout1到zfile.txt 然后,将STDERR 2发送到stdout1本身(使用&1指针)。 因此,1和2都进入同一个文件(zfile.txt)
最后,你可以把所有的东西都打包到nohup命令&中,在后台运行:
nohup zz.sh 1> zfile.txt 2>&1&
其他回答
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.
下面的命令可以让你在后台运行一些东西,而不会得到noup .out:
nohup command |tee &
通过这种方式,你将能够在远程服务器上运行脚本时获得控制台输出:
如果你面前的mac/linux上有一个BASH shell,你可以尝试以下步骤来实际理解重定向:
创建一个名为zz.sh的2行脚本
#!/bin/bash
echo "Hello. This is a proper command"
junk_errorcommand
echo命令的输出进入STDOUT文件流(文件描述符1)。 error命令的输出进入STDERR文件流(文件描述符2)。
目前,只需执行脚本即可将STDOUT和STDERR发送到屏幕。
./zz.sh
现在从标准重定向开始:
zz.sh > zfile.txt
在上面,“echo”(STDOUT)进入zfile.txt。而“错误”(STDERR)显示在屏幕上。
以上同:
zz.sh 1> zfile.txt
现在您可以尝试相反的方法,将“error”STDERR重定向到文件中。来自“echo”命令的STDOUT显示在屏幕上。
zz.sh 2> zfile.txt
将上述两者结合起来,你会得到:
zz.sh 1> zfile.txt 2>&1
解释:
首先,发送stdout1到zfile.txt 然后,将STDERR 2发送到stdout1本身(使用&1指针)。 因此,1和2都进入同一个文件(zfile.txt)
最后,你可以把所有的东西都打包到nohup命令&中,在后台运行:
nohup zz.sh 1> zfile.txt 2>&1&
nohup some_command > /dev/null 2>&1&
这就是你所需要做的!
sudo bash -c "nohup /opt/viptel/viptel_bin/log.sh $* &> /dev/null" &
重定向sudo的输出会导致sudo重新询问密码,因此需要一种笨拙的机制来执行这种变体。