shell:如何使文件尾部在后台运行
我想在 shell 中运行一些任务。
- tail 将一个文件拖入一个新文件:例如:
tail -f debug|tee -a test.log
- 同时运行其他任务。
我的问题是:如何使命令 tail -f debug|tee -a test.log
在后台运行,以便我可以在 shell 脚本中运行其他任务?
I want to run a few tasks in shell.
- tail a file into a new file: for example:
tail -f debug|tee -a test.log
- at the same time, run other tasks.
My question is: how to make the command tail -f debug|tee -a test.log
run in background, so that I can run other tasks then in shell script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为此,您根本不需要 tee,只需使用 shell 的内置追加运算符:
尾随 &在 shell 中正常工作。您只需要 tee 将输出发送到文件以及标准输出,如果您将其放在后台,则可能不是您想要的。
You don't need tee at all for this, just use the shell's built-in append operator:
The trailing & works as normal in the shell. You only need tee to send the output to a file as well as standard out, which if you have it in the background probably isn't what you want.
通常,如果您想将某些内容设置为背景,只需在命令后使用&符号即可。
然后您可以稍后输入
fg
将其返回前台。这回答了你的问题还是我错过了你问的问题?Normally you just use an ampersand after the command if you want to background something.
Then you can bring it back to the foreground later by typing
fg
. Did this answer your question or have I missed what you were asking?执行此操作的简单方法是:
screen
允许您在一个终端连接上运行多个终端会话。您可以使用 Ctrl-A [Backspace]|[Space] 来回切换。创建另一个单独的 shell Ctrl-A cscreen 的一个主要好处是,如果终端会话断开连接,它会保持一切运行。只需关闭终端窗口或断开 ssh 连接,转到另一台计算机,登录并运行 screen -R -D 即可重新连接到仍在运行的所有内容。
如果您只是偶尔需要此功能,只需运行 tail,键入 Ctrl-Z,运行命令,然后
fg %1
将 tail 进程带回前台,或bg %1
code> 使其永久在后台运行。如果您确实使用 Ctrl-Z,则jobs
命令会显示所有分离的作业。The simple way to do this is:
screen
lets you run multiple terminal sessions on one terminal connection. You switch back and forth with Ctrl-A [Backspace]|[Space]. To create another separate shell Ctrl-A cA major benefit of screen is that if the terminal session disconnects, it keeps everything runnning. Just close the terminal window or disconnect ssh, go to another computer, log in and run
screen -R -D
to reconnect to everything which is still running.If you only occasionally need this, just run tail, type Ctrl-Z, run a command, then
fg %1
to bring the tail process back into the foreground, orbg %1
to make it run in the background permanently. If you do use Ctrl-Z, then thejobs
command shows all of your detached jobs.此外,您可以使用命令“jobs”查看正在运行的尾部。例如:
In addition, you can see the running tails with the command 'jobs'. For example: