Bash:打印程序输出到文件,而不是在屏幕上

时间:2018-01-29 16:52:17

标签: linux bash shell

我有一个bash文件可以为我启动一些服务器。我希望运行的服务器生成的所有程序标准输出都保存在文件中,而不是由屏幕上的bash打印。这就是我编写bash文件的方式:

#!bin/sh

rm -rf database.db

go run server.go -typ=server -id=Server1 -host=localhost -port=9998 | tee logs/Server1.log &
go run server.go -typ=server -id=Server2 -host=localhost -port=9999 | tee logs/Server2.log &
go run server.go -typ=server -id=Server3 -host=localhost -port=9998 | tee logs/Server3.log ;

当我运行bash my_script.sh时,输出确实被写入.log文件,但是屏幕上仍然会打印相同的输出。我怎么解决它?

2 个答案:

答案 0 :(得分:2)

tee命令将输出重定向到标准输出和文件。

如果您希望将日志重定向到该文件,请使用>之类的my_command > out_file

答案 1 :(得分:0)

您可以直接将stdout发送到文件,而无需在shell脚本中添加任何tee命令

EG。如果shell脚本名为database.sh 然后在终端执行

./database.sh > output.txt

上述命令将删除output.txt的所有现有内容

或者

./database.sh >> output.txt

这会将输出附加到output.txt的内容(如果有的话)

如果您希望多次运行shell脚本并保留每次运行的日志

,后者将会很有用
相关问题