将脚本中的所有命令 stdout/stderr 重定向到日志文件
我已经阅读了如何单独记录某些脚本或命令,但没有了解如何从 BASH shell 中记录所有命令。我想做的是:
- 用户运行脚本。 (脚本从现在开始将 stdout/stderr 记录到日志文件中)
- 用户执行其他操作/运行其他命令/回显/等,所有这些都记录在日志文件中。
一个不太罗嗦/更小程序的例子:
exec > >(tee logfile.log) 当用户输入时,它正是我想要做的。它将 stdout 记录到 logfile.log 中,并将继续这样做,直到 bash shell 关闭。但是,将这个命令作为脚本运行并不会执行此操作。我想要它。
I've read how to log certain scripts or commands individually, but nothing on how to log all commands from within a BASH shell. What I want to do is:
- User runs script. (script logs stdout/stderr from now on to a logfile)
- User does other stuff/runs other commands/echoes/etc and all of these are logged in logfile.
A less wordy / more codey example:
exec > >(tee logfile.log) when typed in by the user does exactly what I want to do. It logs stdout to logfile.log and will continue to do so until the bash shell is closed. However, running this very command as a script does not do this. I want it to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不能在一个在自己的 shell 下运行的脚本中执行此操作(即它以
#!/bin/bash
开头,你chmod +x
并像调用它一样调用它可执行)。重定向会影响子 shell,但它无法到达父 shell 来执行您想要的操作。您可以.
文件(如../myscript.sh
),它将在您的shell中执行命令,然后您可以重定向内容如你所愿。另一种方法是让你的脚本启动一个子 shell 本身(它将继承 stdin、stdout、stderr)。这就是
script
命令的作用。它将所有内容记录到名为(默认)typescript
的文件中,直到用户退出子 shell。You can't do this in a script that runs under its own shell (i.e. it starts with
#!/bin/bash
and youchmod +x
and invoke it like an executable). The redirect affects the subshell but it can't reach the parent to do what you want. You can.
the file (as in. ./myscript.sh
) which will execute the commands in your shell and then you can redirect things as you want.The other way to do it would be for your script to start a subshell itself (which would inherit stdin, stdout, stderr). This is what the
script
command does. It logs everything to a file named (by default)typescript
until the user exits the subshell.$ bash | tee /tmp/logs/logfile.txt
$ ls /tmp/logs
logfile.txt
$ < CTRL-D>
exit
$ cat /tmp/logs/logfile.txt
logfile.txt
如果您只是寻找标准输出,那么这似乎可行。如果您想要标准输入/标准输出,那么脚本就是前面提到的方法。
$ bash | tee /tmp/logs/logfile.txt
$ ls /tmp/logs
logfile.txt
$ < CTRL-D>
exit
$ cat /tmp/logs/logfile.txt
logfile.txt
if you're looking for just stdout then this seems to work. If you want stdin/stdout then script is the way to go as mentioned previously.
别名怎么样?
How about an alias?
仅供参考,如果有人想这样做来启动守护进程(后台进程),我建议查看优秀的 守护进程。
daemonize
允许您从某个目录启动进程(无需 cd'ing)、重定向 stdout、重定向 stderr、写入 pidfile 或 lockfile,并以特定用户身份运行。这非常有用,例如。当编写你自己的小初始化脚本时。手册页中的概要告诉您有关其相当简单的用法的大部分内容:
Just for reference, if someone wants to do this to start a daemon (background process), i'd suggest to look at the excellent daemonize.
daemonize
allows you to start a process from a certain directory (without cd'ing), redirecting stdout, redirecting stderr, writing a pidfile or lockfile, and run as a certain user. This is very useful eg. when writing your own small init script.synopsis from the man page tells you most about its quite straightforward usage: