首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >'nohup.out‘文件中没有插入日志

'nohup.out‘文件中没有插入日志
EN

Stack Overflow用户
提问于 2020-08-26 12:05:39
回答 3查看 469关注 0票数 0

我通过crontab执行script1.sh。

script1.sh

代码语言:javascript
运行
复制
echo "Crontab is executing this script."
echo "Some code is running here."
cd cron
./script2.sh

“script1.sh”正在调用“script2.sh”。

'script2.sh‘

代码语言:javascript
运行
复制
echo "Exceuting script2."
echo "log of script3.sh is inserting in nohup.out file."
nohup sh script3.sh &
echo "Above syntax is not logging in nohup.out file. but this syntax is 'sh script3.sh > nohup.out &' is logging in nohup.out file. Why is it so?"

“script2.sh”正在调用“script3.sh”,但无法登录nohup.out文件。对于日志记录,使用以下语法。

nohup sh script3.sh &

“script3.sh”包含下面提到的代码行。

代码语言:javascript
运行
复制
echo "Executing script3. This is just test example to simulate the things."

为什么日志没有插入到nohup.out文件中?

EN

回答 3

Stack Overflow用户

发布于 2020-08-30 03:51:41

来自info nohup

23.4‘nohup’:运行对hangups =============================================免疫的命令

‘’nohup‘在忽略挂起信号的情况下运行给定的命令,以便在您注销后该命令可以在后台继续运行。

..。

如果标准输出是终端,则将命令的标准输出附加到‘nohup.out’文件中;

如果无法写入,则执行waltera:将当前目录中的写入权限/空间附加到文件‘$HOME/nohup.out’中;如果无法写入,则不运行命令。

..。

但是,如果关闭了标准输出,则会将标准错误终端输出附加到文件‘nohup.out’或‘$HOME/nohup.out’中,如上所述。

cron执行脚本时,标准输出既不是终端,也不是封闭的,因此nohup没有理由重定向任何输出。

没有终端的nohup的另一个demo是当您使用另一个nohup启动您的script2.sh时,它有一个nohup命令

代码语言:javascript
运行
复制
nohup ./script2.sh > masternohup.out

script3.sh的输出将写入masternohup.out

票数 4
EN

Stack Overflow用户

发布于 2020-08-26 14:11:46

来自man nohup

代码语言:javascript
运行
复制
...
If standard input is a terminal, redirect it from an unreadable file.
If standard output is a terminal, append output to 'nohup.out' if possible, '$HOME/nohup.out' otherwise.
If standard error is a terminal, redirect it to standard output.
To save output to FILE, use 'nohup COMMAND > FILE'.
...

所以它可以是$HOME/nohup.out中的任何一种,最好使用它来控制输出:

代码语言:javascript
运行
复制
nohup COMMAND &> FILE
票数 1
EN

Stack Overflow用户

发布于 2020-08-30 03:59:35

在现代,你不需要nohup

Shell转义方法允许进程离开其进程组,并且永远不会收到SIGHUP或任何其他指向进程组的信号。

在bash shell中:

代码语言:javascript
运行
复制
(command &>log.txt &)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63590342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档