shell:如何使文件尾部在后台运行

发布于 2024-11-19 22:24:59 字数 223 浏览 2 评论 0原文

我想在 shell 中运行一些任务。

  1. tail 将一个文件拖入一个新文件:例如:tail -f debug|tee -a test.log
  2. 同时运行其他任务。

我的问题是:如何使命令 tail -f debug|tee -a test.log 在后台运行,以便我可以在 shell 脚本中运行其他任务?

I want to run a few tasks in shell.

  1. tail a file into a new file: for example: tail -f debug|tee -a test.log
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

¢好甜 2024-11-26 22:24:59

为此,您根本不需要 tee,只需使用 shell 的内置追加运算符:

tail -f debug >> test.log &

尾随 &在 shell 中正常工作。您只需要 tee 将输出发送到文件以及标准输出,如果您将其放在后台,则可能不是您想要的。

You don't need tee at all for this, just use the shell's built-in append operator:

tail -f debug >> test.log &

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.

你在看孤独的风景 2024-11-26 22:24:59

通常,如果您想将某些内容设置为背景,只需在命令后使用&符号即可。

tail -f debug|tee -a test.log &

然后您可以稍后输入 fg 将其返回前台。这回答了你的问题还是我错过了你问的问题?

Normally you just use an ampersand after the command if you want to background something.

tail -f debug|tee -a test.log &

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?

层林尽染 2024-11-26 22:24:59

执行此操作的简单方法是:

screen -R -D
tail -f debug|tee -a test.log
Ctrl-A c
ps ax |grep tail
Ctrl-A [Backspace]
...
Ctrl-A [Spacebar]

screen 允许您在一个终端连接上运行多个终端会话。您可以使用 Ctrl-A [Backspace]|[Space] 来回切换。创建另一个单独的 shell Ctrl-A c

screen 的一个主要好处是,如果终端会话断开连接,它会保持一切运行。只需关闭终端窗口或断开 ssh 连接,转到另一台计算机,登录并运行 screen -R -D 即可重新连接到仍在运行的所有内容。

如果您只是偶尔需要此功能,只需运行 tail,键入 Ctrl-Z,运行命令,然后 fg %1 将 tail 进程带回前台,或 bg %1 code> 使其永久在后台运行。如果您确实使用 Ctrl-Z,则 jobs 命令会显示所有分离的作业。

The simple way to do this is:

screen -R -D
tail -f debug|tee -a test.log
Ctrl-A c
ps ax |grep tail
Ctrl-A [Backspace]
...
Ctrl-A [Spacebar]

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 c

A 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, or bg %1 to make it run in the background permanently. If you do use Ctrl-Z, then the jobs command shows all of your detached jobs.

各空 2024-11-26 22:24:59

此外,您可以使用命令“jobs”查看正在运行的尾部。例如:

[root@OS4 tufin-admin]# tail -F /var/log/to.log > /tmp/to &
[3] 674697
[root@OS4 tufin-admin]# jobs -pl
[1]  650033 Running                 kubectl logs -f -l app=rma > /tmp/rma &
[2]- 674286 Running                 tail -F var/log/journal/d1524a98a19d4ab59b9ccf9749e3d595/system@d439ce74822b4836ab58a88b6a65d200-0000000000014a12-00060d1140dd3307.journal > /tmp/journal &
[3]+ 674697 Running                 tail -F /var/log/to.log > /tmp/to &

In addition, you can see the running tails with the command 'jobs'. For example:

[root@OS4 tufin-admin]# tail -F /var/log/to.log > /tmp/to &
[3] 674697
[root@OS4 tufin-admin]# jobs -pl
[1]  650033 Running                 kubectl logs -f -l app=rma > /tmp/rma &
[2]- 674286 Running                 tail -F var/log/journal/d1524a98a19d4ab59b9ccf9749e3d595/system@d439ce74822b4836ab58a88b6a65d200-0000000000014a12-00060d1140dd3307.journal > /tmp/journal &
[3]+ 674697 Running                 tail -F /var/log/to.log > /tmp/to &
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文