简体   繁体   English

nohup append output 文件顶部的执行命令

[英]nohup append the executed command at the top of the output file

Let's say that we invoke the nohup in the following way:假设我们通过以下方式调用nohup

nohup foo.py -n 20 2>&1 &

This will write the output to the nohup.out .这会将 output 写入nohup.out How could we achieve to have the whole command nohup foo.py -n 20 2>&1 & sitting at the top of the nohup.out (or any other specified output file) after which the regular output of the executed command will be written to that file?我们如何才能让整个命令nohup foo.py -n 20 2>&1 &坐在nohup.out (或任何其他指定的 output 文件)的顶部,之后将写入执行命令的常规 output那个文件?

The reason for this is for purely debugging purpose as there will be thousands of commands like this executed and very often some of them will crash due to various reasons.这样做的原因是纯粹出于调试目的,因为将执行数千个这样的命令,并且经常由于各种原因其中一些会崩溃。 It's like a basic report kept in a file with the executed command written at the top followed by the output of the executed command.这就像保存在文件中的基本报告,执行命令写在顶部,然后是执行命令的 output。

A straightforward alternative would be something like:一个简单的替代方案是这样的:

myNohup() {
  (
    set +m                         # disable job control
    [[ -t 0 ]] && exec </dev/null  # redirect stdin away from tty
    [[ -t 1 ]] && exec >nohup.out  # redirect stdout away from tty
    [[ -t 2 ]] && exec 2>&1        # redirect stderr away from tty
    set -x                         # enable trace logging of all commands run
    "$@"                           # run our arguments as a command
  ) & disown -h "$!"               # do not forward any HUP signal to the child process
}

To define a command we can test this with:要定义命令,我们可以使用以下命令进行测试:

waitAndWrite() { sleep 5; echo "finished"; }

...and run: ...并运行:

myNohup waitAndWrite

...will return immediately and, after five seconds, leave the following in nohup.out : ...将立即返回,五秒钟后,在nohup.out中保留以下内容:

+ waitAndWrite
+ sleep 5
+ echo finished
finished

If you only want to write the exact command run without the side effects of xtrace, replace the set -x with (assuming bash 5.0 or newer) printf '%s\n' "${*@Q}" .如果您只想编写没有 xtrace 副作用的确切命令运行,请将set -x替换为(假设 bash 5.0 或更高版本) printf '%s\n' "${*@Q}"

For older versions of bash, you might instead consider printf '%q ' "$@"; printf '\n'对于旧版本的 bash,您可以考虑printf '%q ' "$@"; printf '\n' printf '%q ' "$@"; printf '\n' . printf '%q ' "$@"; printf '\n'


This does differ a little from what the question proposes:这确实与问题提出的有点不同:

  • Redirections and other shell directives are not logged by set -x . set -x不记录重定向和其他 shell 指令。 When you run nohup foo 2>&1 & , the 2>&1 is not passed as an argument to nohup ;当您运行nohup foo 2>&1 &时, 2>&1不会作为参数传递给nohup instead, it's something the shell does before nohup is started .相反,这是 shellnohup启动之前所做的事情。 Similarly, the & is not an argument but an instruction to the shell not to wait() for the subprocess to finish before going on to future commands.同样, &不是参数,而是对 shell 的指令,在继续执行未来命令之前不要wait()子进程完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM