Bash 脚本不输出 nohup.out + Jenkins

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16902567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:35:39  来源:igfitidea点击:

Bash script not outputting nohup.out + Jenkins

bashjenkinsnohup

提问by gjw80

I have a bash script that I am calling from a build step in Jenkins. Within this bash script is a nohup command for calling a different script in the background, such as:

我有一个 bash 脚本,我正在 Jenkins 的构建步骤中调用它。在这个 bash 脚本中是一个 nohup 命令,用于在后台调用不同的脚本,例如:

#!/bin/bash
nohup otherScript.sh &

After the build step completes I go to the path where the nohup.out should have been created, but there is nothing there. Any ideas on what is going on?

构建步骤完成后,我转到应该创建 nohup.out 的路径,但那里什么也没有。关于发生了什么的任何想法?

回答by gareth_bowles

You should make sure that the output goes into your build's workspace. This will avoid permission problems with other directories.

您应该确保输出进入您的构建工作区。这将避免其他目录的权限问题。

nohup otherScript.sh > $WORKSPACE/scriptOutput.txt 2>&1 &

回答by Ansgar Wiechers

Quoting from man nohup:

引自man nohup

If standard input is a terminal, redirect it from /dev/null. If standard output is a terminal, append output to nohup.outif possible, $HOME/nohup.outotherwise. If standard error is a terminal, redirect it to standard output. To save output to FILE, use nohup COMMAND > FILE.

如果标准输入是终端,则从 /dev/null 重定向它。如果标准输出是终端,nohup.out则尽可能将输出附加到,$HOME/nohup.out否则。如果标准错误是终端,则将其重定向到标准输出。要将输出保存到 FILE,请使用nohup COMMAND > FILE.

Running the command from Jenkins probably means that STDOUTis not a terminal, thus nohup.outis not created. As gareth_bowlesalready suggested, you should redirect the output to a file with a defined path:

从 Jenkins 运行命令可能意味着它STDOUT不是终端,因此nohup.out不会被创建。正如gareth_bowles已经建议的那样,您应该将输出重定向到具有定义路径的文件:

nohup script.sh >/path/to/output.log 2>&1 &