Bash 脚本 - 运行 处理并在正常情况下发送到后台,否则
Bash script - run process & send to background if good, or else
我需要启动一个 Golang 网络服务器,并通过 bash 脚本将其 运行 留在后台。如果有问题的脚本在语法上是正确的(大多数时候都是这样),这只是发出
的问题
go run /path/to/index.go &
但是,我必须考虑到 index.go 可能存在某种错误。我应该解释一下,在 Golang 中,对于 "trival" 之类的东西,导入一个你随后无法使用的模块。在这种情况下,go run /path/to/index.go
位将 return 一条错误消息。在终端中,这将类似于
index.go:4:10: expected...
我需要做的是以某种方式更改上面的命令,以便我可以将任何错误消息汇集到文件中以供稍后检查。我尝试了 go run /path/to/index.go >> errors.txt
的变体,在不同的位置终止 & 但无济于事。
我怀疑有一种 bash 方法可以通过一些明智地使用 braces/brackets 等改变命令评估的优先级来做到这一点。然而,这超出了我的 bash 能力。如果有人能提供帮助,我将不胜感激。
更新
几分钟后...经过更多实验,我发现这有效
go run /path/to/index.go &> errors.txt &
除了我实际上不明白它为什么起作用之外,还有一个问题是当命令完成时它会生成一个 0 字节 errors.txt 文件,而 Golang 不会抛出任何错误消息.有人可以阐明正在发生的事情以及如何改进吗?
摘自 man bash
.
Redirecting Standard Output and Standard Error
This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word.
There are two formats for redirecting standard output and standard error:
&>word
and
>&word
Of the two forms, the first is preferred. This is semantically equivalent to
>word 2>&1
Appending Standard Output and Standard Error
This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be appended to the file whose name is the expansion of word.
The format for appending standard output and standard error is:
&>>word
This is semantically equivalent to
>>word 2>&1
Narūnas K 的回答涵盖了 &>
重定向有效的原因。
无论如何都会创建文件的原因是 shell 在运行相关命令之前就创建了文件。
您可以通过尝试 no-such-command > file.out
看到这一点,即使出现 shell 错误,因为 no-such-command
不存在,文件也会被创建(使用 &>
该测试将在文件中得到 shell 的错误。
这就是为什么您不能执行 sed 'pattern' file > file
之类的操作来就地编辑文件的原因。
我需要启动一个 Golang 网络服务器,并通过 bash 脚本将其 运行 留在后台。如果有问题的脚本在语法上是正确的(大多数时候都是这样),这只是发出
的问题go run /path/to/index.go &
但是,我必须考虑到 index.go 可能存在某种错误。我应该解释一下,在 Golang 中,对于 "trival" 之类的东西,导入一个你随后无法使用的模块。在这种情况下,go run /path/to/index.go
位将 return 一条错误消息。在终端中,这将类似于
index.go:4:10: expected...
我需要做的是以某种方式更改上面的命令,以便我可以将任何错误消息汇集到文件中以供稍后检查。我尝试了 go run /path/to/index.go >> errors.txt
的变体,在不同的位置终止 & 但无济于事。
我怀疑有一种 bash 方法可以通过一些明智地使用 braces/brackets 等改变命令评估的优先级来做到这一点。然而,这超出了我的 bash 能力。如果有人能提供帮助,我将不胜感激。
更新
几分钟后...经过更多实验,我发现这有效
go run /path/to/index.go &> errors.txt &
除了我实际上不明白它为什么起作用之外,还有一个问题是当命令完成时它会生成一个 0 字节 errors.txt 文件,而 Golang 不会抛出任何错误消息.有人可以阐明正在发生的事情以及如何改进吗?
摘自 man bash
.
Redirecting Standard Output and Standard Error
This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word. There are two formats for redirecting standard output and standard error: &>word and >&word Of the two forms, the first is preferred. This is semantically equivalent to >word 2>&1
Appending Standard Output and Standard Error
This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be appended to the file whose name is the expansion of word. The format for appending standard output and standard error is: &>>word This is semantically equivalent to >>word 2>&1
Narūnas K 的回答涵盖了 &>
重定向有效的原因。
无论如何都会创建文件的原因是 shell 在运行相关命令之前就创建了文件。
您可以通过尝试 no-such-command > file.out
看到这一点,即使出现 shell 错误,因为 no-such-command
不存在,文件也会被创建(使用 &>
该测试将在文件中得到 shell 的错误。
这就是为什么您不能执行 sed 'pattern' file > file
之类的操作来就地编辑文件的原因。